Skip to content

Commit

Permalink
Added model tests, user.rb so it has many articles.
Browse files Browse the repository at this point in the history
  • Loading branch information
paulmckissock committed Jun 13, 2024
1 parent bae59b5 commit 19d0e06
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 6 deletions.
2 changes: 1 addition & 1 deletion app/models/user.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class User < ApplicationRecord
has_secure_password
has_many :article, foreign_key: "author_id"
has_many :articles, foreign_key: "user_id"

validates :name, presence: true, uniqueness: true
validates :email, presence: true, uniqueness: true
Expand Down
2 changes: 1 addition & 1 deletion test/controllers/users_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
class UsersControllerTest < ActionDispatch::IntegrationTest
include FactoryBot::Syntax::Methods

def setup
setup do
@user = create(:user)
@valid_user_params = {user: attributes_for(:user)}
@invalid_user_params = {user: {name: "", email: "invalid", password: "pass", password_confirmation: "word"}}
Expand Down
2 changes: 1 addition & 1 deletion test/factories/articles.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
factory :article do
title { "Ruby on Rails" }
link { "https://rubyonrails.org/" }
association :user
association :user, factory: :user
end
end
13 changes: 10 additions & 3 deletions test/models/article_test.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
require "test_helper"

class ArticleTest < ActiveSupport::TestCase
include FactoryBot::Syntax::Methods

setup do
@article = FactoryBot.build(:article)
@article = build(:article)
end

test "should be valid with valid attributes" do
Expand All @@ -11,11 +13,16 @@ class ArticleTest < ActiveSupport::TestCase

test "should not be valid without a title" do
@article.title = nil
assert_not @article.valid?
assert_not @article.valid?, "Article is valid without a title"
end

test "should not be valid without a link" do
@article.link = nil
assert_not @article.valid?
assert_not @article.valid?, "Article is valid without a link"
end

test "should not be valid without a user" do
@article.user = nil
assert_not @article.valid?, "Article is valid without a user"
end
end
44 changes: 44 additions & 0 deletions test/models/user_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
require "test_helper"

class UserTest < ActiveSupport::TestCase
include FactoryBot::Syntax::Methods
setup do
@user = create(:user)
end

test "should be valid" do
assert @user.valid?
end

test "name should be present" do
@user.name = ""
assert_not @user.valid?
end

test "email should be present" do
@user.email = ""
assert_not @user.valid?
end

test "name should be unique" do
duplicate_user = @user.dup
@user.save
assert_not duplicate_user.valid?
end

test "email should be unique" do
duplicate_user = @user.dup
duplicate_user.email = @user.email.upcase
@user.save
assert_not duplicate_user.valid?
end

test "password should be present (nonblank)" do
@user.password = @user.password_confirmation = ""
assert_not @user.valid?
end

test "should have many articles" do
assert_respond_to @user, :articles
end
end

0 comments on commit 19d0e06

Please sign in to comment.