Rails uniqueness validation test failing - ruby-on-rails

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.

Related

Rails minitest, why is my article model invalid?

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

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

uniqueness test on minitest

I use minitest on Ruby on Rails. Below is my model.
require 'mongoid'
class Person
include Mongoid::Document
index({ pin: 1 }, { unique: true, name: "pin_index" })
field :first_name
field :last_name
field :pin
validates :pin, presence: true, uniqueness: true
validates :first_name, presence: true
validates :last_name, presence: true
end
I try to write model test.I want to write a test that controls whether pin field is unique or not. How can i do this? Any idea?
I try to write a test like below:
it 'must not be valid' do
person_copy = person.dup
person.save
person_copy.save
end
You can write the test like this:
it 'must have unique pin' do
person_copy = person.dup
proc { person_copy.save! }.must_raise(Mongoid::Errors::Validations)
person_copy.errors.must_include(:pin)
end
You can use assert_includes and assert_same to test the error is the right one (about uniqueness):
it 'must not be valid' do
person_copy = person.dup
person.save
person_copy.save
assert_includes person.errors, :pin
assert_same person.errors[:pin], "pin is not unique (replace with actual error message)"
end
Considering you have a fixture already set, you can just do this:
test 'pin must be unique' do
new_person = Person.new(#person.attributes)
refute new_person.valid?
end

Lowercase condition in "magic" methods for Model

A model Country has a attribute code which is automatically converted to lowercase by a before_save callback. Is it possible to force this behaviour on "magic" methods without rewriting large chunks of ActiveRecord::Base?
class Country < ActiveRecord::Base
attr_accessible :code
validates :code, :presence => true
validates_uniqueness_of :code, :case_sensitive => false
before_save do |country|
country.code.downcase! unless country.code.nil?
end
end
RSpec
describe Country do
describe 'data normalization'
before :each do
#country = FactoryGirl.create(:country, :code => 'DE')
end
# passes
it 'should normalize the code to lowercase on insert' do
#country.code.should eq 'de'
end
# fails
it 'should be agnostic to uppercase finds' do
country = Country.find_by_code('DE')
country.should_not be_nil
end
# fails
it 'should be agnostic to uppercase finds_or_creates' do
country = Country.find_or_create_by_code('DE')
country.id.should_not be_nil # ActiveRecord Bug?
end
end
This is what I came up with, altough I really hate that approach (as mentioned in the question). An easy alternative would be to set the column, table or whole database up to ignore case (but this is db dependendt).
class Country < ActiveRecord::Base
attr_accessible :code
validates :code, :presence => true
validates_uniqueness_of :code, :case_sensitive => false
before_save do |country|
country.code.downcase! unless country.code.nil?
end
class ActiveRecord::Base
def self.method_missing_with_code_finders(method_id, *arguments, &block)
if match = (ActiveRecord::DynamicFinderMatch.match(method_id) || ActiveRecord::DynamicScopeMatch.match(method_id))
attribute_names = match.attribute_names
if code_index = attribute_names.find_index('code')
arguments[code_index].downcase!
end
end
method_missing_without_code_finders(method_id, *arguments, &block)
end
class << self
alias_method_chain(:method_missing, :code_finders)
end
end
end

Resources