Rails minitest, why is my article model invalid? - ruby-on-rails

I am starting to get into testing with minitest in Rails. Currently my first test to see if my model is valid is returning false. The error message itself is also very generic Expected false to be truthy. I have tested everything else and all of those tests work fine. Does anybody know what could be causing this?
article_test.rb
require 'test_helper'
class ArticleTest < ActiveSupport::TestCase
def setup
#article = Article.new(title:"Avengers Endgame", body:"I am inevitable")
end
test "article should be valid" do
assert #article.valid?
end
test "title should be present" do
#article.title = " "
assert_not #article.valid?
end
test "body should not be too short" do
#article.body = "aa"
assert_not #article.valid?
end
end
article.rb
class Article < ApplicationRecord
include Visible
belongs_to :user
has_many :comments, dependent: :destroy
has_rich_text :body
validates :title, presence: true
validates :body, presence: true, length: { minimum: 10 }
end

The main problem here is that you're using a poor method for testing your validations.
assert #article.valid? and assert_not #article.valid? does not actually tell you anything at all about the object under test. When the test fails you're none the wiser about why it actually failed and if the failure is actually even connected to what you're proportedly testing. At best it serves as a sort of litmus test that your test setup is correct.
Instead of this "carpet bombing" approach test each validation on its own:
class ArticleTest < ActiveSupport::TestCas
test "title should be present" do
article = Article.new(title: '')
article.valid?
assert_includes build_article.errors[:title], "can’t be blank"
end
test "body should not be too short" do
article = Article.new(body: 'aa')
article.valid?
assert_includes article.errors[:body], "is too short"
end
end
Testing all the validations at once (creating a record with valid input) will be covered by your integration and system tests anyways.

You have belongs_to :user, which expects the #article to have a user_id compulsorily to be present, before it can be saved.
If user_id is optional during creation, change this:
belongs_to :user
to
belongs_to :user, optional: true

Related

Ruby On Rails Model Assert Test failing

Currently learning ROR via The Odin Project Curriculum. As part of the 'PROJECT: Building with active record' I have set up three basic models: User, Post, Comments.
In both Post and Comments my basic 'Post/Comments is valid' test is failing(false expecting truthy), but all specific tests pass, e.g. code for PostTest:
require 'test_helper'
class PostTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
def setup
#post = Post.new(title: "Hello World", body: "Hello world, it's nice to
meet you", user_id: 1)
end
test "Post is valid" do
assert #post.valid?
end
test "Post title is not blank" do
#post.title = ""
assert_not #post.valid?
end
test "Post title is less than 40 char" do
#post.title = "a"*51
assert_not #post.valid?
end
test "Post body is not blank" do
#post.body = ""
assert_not #post.valid?
end
test "Post has user_id" do
#post.user_id = ""
assert_not #post.valid?
end
end
The reason I am confused is that if I use rails console to manually create the same Post as in the #setup above then the valid? method correctly returns true. Additionally the User model setup with exactly the same formation of #setup and user is valid tests passes fine. The only difference I see is that User has_many :posts where as the Posts belong_to :user
For ref the Post model is:
class Post < ApplicationRecord
validates :title, presence: true, length: {maximum: 40}
validates :body, presence: true
validates :user_id, presence: true
belongs_to :user
has_many :comments
end
Are you using fixtures for your users or how do you know a user record with id 1 exists?
if you are using fixtures, you should create a user in your setup:
def setup
user = users(:default_user)
#post = Post.new(
title: "Hello World",
body: "Hello world, it's nice to meet you",
user: user
)
end
See more on fixtures: http://guides.rubyonrails.org/testing.html#the-low-down-on-fixtures

Testing validations in model using RSpec with Rails

I'm super new to testing my app using RSpec and I'm trying to test the validation of a comment without a user and keep getting syntax errors.
Here is the comment model code.
class Comment < ApplicationRecord
belongs_to :user
belongs_to :product
scope :rating_desc, -> { order(rating: :desc) }
validates :body, presence: true
validates :user, presence: true
validates :product, presence: true
validates :rating, numericality: { only_integer: true }
after_create_commit { CommentUpdateJob.perform_later(self, user) }
end
and here is the comment spec:
require 'rails_helper'
describe Comment do
before do
#product = Product.create!(name: "race bike", description: "fast race bike")
#user = User.create!(email: "jerryhoglen#me.com", password: "Maggie1!")
#product.comments.create!(rating: 1, user: #user, body: "Awful bike!")
end
it "is invalid without a user"
expect(build(:comment, user:nil)).to_not be_valid
end
end
What you're doing here is good - building objects and using the be_valid matcher. But if you use shoulda-matchers there's a one-liner to test a model validation:
describe Comment do
it { is_expected.to validate_presence_of :user }
end
You can do this for other validations such as uniqueness, numericality, etc, though you'd have to look up the syntax.
you missed a do, do it like:
it "is invalid without a user" do
expect(build(:comment, user: nil)).to_not be_valid
end
But that's not a very clear test when it fails, I suggest you check the actual expected validation error.
That's what it may look like:
expect(ValidatingWidget.new.errors_on(:name)).to include("can't be blank")
expect(ValidatingWidget.new(:name => "liquid nitrogen")).to have(0).errors_on(:name)
See rspec-rails errors_on # relishapp

Rails validation test not passing

In our Rails app, we have the following models:
class User < ActiveRecord::Base
has_many :administrations, dependent: :destroy
has_many :calendars, through: :administrations
end
class Administration < ActiveRecord::Base
belongs_to :user
belongs_to :calendar
end
class Calendar < ActiveRecord::Base
has_many :administrations, dependent: :destroy
has_many :users, through: :administrations
end
We tried to validate the Administration model with the following administration_test.rb test file:
require 'test_helper'
class AdministrationTest < ActiveSupport::TestCase
def setup
#user = users(:noemie)
#administration = Administration.new(user_id: #user.id, calendar_id: #calendar_id)
end
test "should be valid" do
assert #administration.valid?
end
test "user id should be present" do
#administration.user_id = nil
assert_not #administration.valid?
end
test "calendar id should be present" do
#administration.calendar_id = nil
assert_not #administration.valid?
end
end
When we run the test, we get the following results:
FAIL["test_calendar_id_should_be_present", AdministrationTest, 2015-06-30 07:24:58 -0700]
test_calendar_id_should_be_present#AdministrationTest (1435674298.26s)
Expected true to be nil or false
test/models/administration_test.rb:21:in `block in <class:AdministrationTest>'
FAIL["test_user_id_should_be_present", AdministrationTest, 2015-06-30 07:24:58 -0700]
test_user_id_should_be_present#AdministrationTest (1435674298.27s)
Expected true to be nil or false
test/models/administration_test.rb:16:in `block in <class:AdministrationTest>'
We are kind of lost: is this the right way to right the test?
If no, how should we write it?
If yes, how can we make it pass?
The problem is not your test but rather that you are expecting the wrong outcome.
belongs_toin ActiveRecord does not add a validation, the macro simply creates a relation.
To validate a relation you would use validates_associated which calls #valid? on each of the associated records and validates_presence_of to ensure that the associated record is present.
class Administration < ActiveRecord::Base
belongs_to :user
belongs_to :calendar
validates_associated :user
validates :user, presence: true
end
When testing validations it is better to write on the assertions on the errors hash, as assert_not #administration.valid? will give a false positive if the validation fails for any other reason.
Bad:
test "user id should be present" do
#administration.user_id = nil
assert_not #administration.valid?
end
Good:
test "user id should be present" do
#administration.user_id = nil
#administration.valid?
assert #administration.errors.key?(:user)
end
Ok, we found the solution.
We updated our Administration model as follows:
class Administration < ActiveRecord::Base
belongs_to :user
belongs_to :calendar
validates :user_id, presence: true
validates :calendar_id, presence: true
end
And also edited our administration_test.rb file:
require 'test_helper'
class AdministrationTest < ActiveSupport::TestCase
def setup
#user = users(:noemie)
#calendar = calendars(:one)
# This code is not idiomatically correct.
#administration = Administration.new(user_id: #user.id, calendar_id: #calendar.id)
end
test "should be valid" do
assert #administration.valid?
end
test "user id should be present" do
#administration.user_id = nil
assert_not #administration.valid?
end
test "calendar id should be present" do
#administration.calendar_id = nil
assert_not #administration.valid?
end
end
The tests are now passing just fine.

Rails uniqueness validation test failing

I'm starting out with Rails 4.2, and I'm try to test uniqueness for the Item models I'm making, I ran this code:
item.rb:
class Item < ActiveRecord::Base
attr_accessor :name
validates :name, uniqueness: true #, other validations...
end
item_test.rb:
require 'test_helper'
class ItemTest < ActiveSupport::TestCase
def setup
#item = Item.new(name: "Example Item")
end
test "name should be unique" do
duplicate_item = #item.dup
#item.save
assert_not duplicate_item.valid?
end
end
but the test didn't pass, saying that the assert_not line is coming out true when it should be nil or false. I basically got this code from a tutorial but cannot figure out why it's not passing. Any help?
Edit: I found the solution, by not defining the other members (specifically :price ) of #item that I defined in the setup action, the test passed. However now I don't know how to make it pass with the the :price member. Below is the full implementation of item.rb & item_test.rb.
item.rb:
class Item < ActiveRecord::Base
attr_accessor :name, :description, :price
validates :name, presence: true, uniqueness: true, length: { maximum: 100 }
validates :description, presence: true,
length: { maximum: 1000 }
VALID_PRICE_REGEX = /\A\d+(?:\.\d{0,2})?\z/
validates :price, presence: true,
:format => { with: VALID_PRICE_REGEX },
:numericality => {:greater_than => 0}
end
item_test.rb:
require 'test_helper'
class ItemTest < ActiveSupport::TestCase
def setup
#item = Item.new(name: "Example Item", description: "Some kind of item.", price: 1.00)
end
test "name should be unique" do
duplicate_item = #item.dup
#item.save
assert_not duplicate_item.valid?
end
end
Almaron's answer above is correct and should be the accepted answer.
I am adding this answer to elaborate on it.
The test would be as follows:
require 'test_helper'
class ItemTest < ActiveSupport::TestCase
def setup
#item = Item.create(name: "Example Item")
end
test "name should be unique" do
duplicate_item = #item.dup
assert_not duplicate_item.valid?
end
end
Note: duplicate_item need not be saved before validating it.
The uniqueness validation is performed against the records already existing in the database. And your Item.new(name: "Example Item") is not in the database untill it is saved. So if you use Item.create(name: "Example Item") instead, the test should pass.
You've identified at least some of the problem in your edit.
The problem isn't that you're using Item.new instead of Item.create the problem is that when you do #item.save the #item record isn't being saved because it has other validation issues.
You could try...
#item.save(validate: false)
... which will force #item to be written to the database, but the test doesn't really determine why the duplicate_item record is invalid.
Better might be to test that you have an error relating to name...
require 'test_helper'
class ItemTest < ActiveSupport::TestCase
def setup
#item = Item.new(name: "Example Item")
end
test "name should be unique" do
duplicate_item = #item.dup
#item.save(validate: false)
duplicate_item.valid? # need this to populate errors
assert duplicate_item.errors
assert duplicate_item.errors[:name]
end
end
I fixed it, I got rid of the attr_accessor line, the test was then able to access the attributes and was able to detect the duplication.

error unit test: underfined assert_not

i have two error when i run rake test:
1)Error
test_should_require_a_following_id(RelationTest)
NoMethodError:underfined method 'assert_not' for #<RelationTest:0x3dd0058>
2)Error
test_should_require_a_follower_id(RelationTest)
NoMethodError:underfined method 'assert_not' for #<RelationTest:0x3c1d700>
this is my relation_test.rb
require 'test_helper'
class RelationTest < ActiveSupport::TestCase
def setup
#relation = Relation.new(follower_id: 9, following_id: 10)
end
test "should be valid" do
assert #relation.valid?
end
test "should require a follower_id" do
#relation.follower_id = nil
assert_not #relation.valid?
end
test "should require a following_id" do
#relation.following_id = nil
assert_not #relation.valid?
end
end
my relation model
class Relation < ActiveRecord::Base
attr_accessible :following_id, :follower_id
belongs_to :follower, :class_name => "User"
belongs_to :following, :class_name => "User"
validates :follower_id, presence: true
validates :following_id, presence: true
end
What can i do to fix that, please help me!
I think the problem is my rail vesion(3.2.19), but assert #relation.valid?still work??
assert_not has been added in rails 4.0.2. You need to do this instead:
assert !#relation.valid?

Resources