diff --git a/app/models/user.rb b/app/models/user.rb index 75ff4c7..22d7338 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -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 diff --git a/test/controllers/users_controller_test.rb b/test/controllers/users_controller_test.rb index c6b66b9..354861f 100644 --- a/test/controllers/users_controller_test.rb +++ b/test/controllers/users_controller_test.rb @@ -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"}} diff --git a/test/factories/articles.rb b/test/factories/articles.rb index 397e740..9bc0c06 100644 --- a/test/factories/articles.rb +++ b/test/factories/articles.rb @@ -2,6 +2,6 @@ factory :article do title { "Ruby on Rails" } link { "https://rubyonrails.org/" } - association :user + association :user, factory: :user end end diff --git a/test/models/article_test.rb b/test/models/article_test.rb index 268e7da..22ef8ec 100644 --- a/test/models/article_test.rb +++ b/test/models/article_test.rb @@ -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 @@ -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 diff --git a/test/models/user_test.rb b/test/models/user_test.rb index e69de29..348b16a 100644 --- a/test/models/user_test.rb +++ b/test/models/user_test.rb @@ -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