Consider the following code:
campaign_spec.rb:
describe Campaign do
before :each do
#campaign = Campaign.new
end
it { should validate_presence_of :position }
end
campaign.rb:
class Campaign < ActiveRecord::Base
attr_accessible :position
validates :position, presence: true
default_scope order(:position)
before_validation :next_position
# sets the position to the next id (1 if none exist) before validation
def next_position
if self.position.blank?
self.position = Campaign.select("coalesce(max(position),0) + 1 as position").reorder(nil).first.position
end
end
end
spec output:
Failures:
1) Campaign should require position to be set
Failure/Error: it { should validate_presence_of :position }
Expected errors to include "can't be blank" when position is set to nil, got errors: ["name can't be blank (nil)"]
# ./spec/models/campaign_spec.rb:9:in `block (2 levels) in <top (required)>'
From what my model is saying, I should be able to create a Campaign without giving it a position. It sets the position if none exists before validation. So why is my spec not passing? I'm thinking that maybe it's not calling my before_validation method?
should validate_presence_of sets the value of the attribute to nil and then runs the validations. Since it is nil, nil.blank? returns true, so your callback is executed, setting it back to some value, and hence validation is found not to work.
Fact is that you don't need to validate this field if you are sure it is never empty. Instead, write the test to check whether it is indeed automatically populated when set to nil.
it 'populates position if blank before validations' do
subject.position = nil
subject.valid?
subject.position.should_not be_nil
end
Related
I have the following task model:
class Task < ApplicationRecord
validates :body, presence: true, length: { minimum: 10 }
validates :complete, presence: true
end
and following FactoryGirl object creation code:
FactoryGirl.define do
factory :incomplete_task, class: :Task do
body { Faker::Pokemon.name + Faker::Pokemon.name }
complete false
factory :complete_task do
complete true
end
end
end
In my Tasks controller tests, I have:
describe '#update' do
it 'toggles completion' do
incomplete_task = create :incomplete_task
toggle_completion(incomplete_task)
expect(incomplete_task.complete).to be_true
end
end
However, this fails because the field 'complete' is missing from the task object created by FG:
Failures:
1) TasksController#update toggles completion
Failure/Error: incomplete_task = create :incomplete_task
ActiveRecord::RecordInvalid:
Validation failed: Complete can't be blank
What is going on here? I'm properly setting the complete attribute, and body is checking out fine. Here is the task schema as well:
# Table name: tasks
#
# id :integer not null, primary key
# created_at :datetime not null
# updated_at :datetime not null
# body :text
# complete :boolean
In ruby, false is considered a blank (not present) value (along with nil, empty string/array and other blank values). Therefore, the presence validator rightfully rejects that record.
The documentation has the following comment:
If you want to validate the presence of a boolean field (where the real values are true and false), you will want to use validates_inclusion_of :field_name, in: [true, false].
This is due to the way Object#blank? handles boolean values: false.blank? # => true.
Using shoulda and FactoryGirl to test model validation.
Factory
FactoryGirl.define do
factory :tag do
value { Faker::Lorem.word }
user
end
end
Tag Model
class Tag < ApplicationRecord
validates :value,
presence: true,
uniqueness: { case_sensitive: false }
belongs_to :user
has_and_belongs_to_many :cards
end
Tag Spec
RSpec.describe Tag, type: :model do
describe 'validations' do
it { should validate_presence_of(:value) }
it { should validate_uniqueness_of(:value) }
end
describe 'associations' do
it { should belong_to(:user) }
# it { should have_and_belong_to_many(:cards) }
end
end
I get the following error when I run the test,
Failures:
1) Tag validations should validate that :value is case-sensitively unique
Failure/Error: it { should validate_uniqueness_of(:value) }
Tag did not properly validate that :value is case-sensitively unique.
After taking the given Tag, setting its :value to ‹"an arbitrary
value"›, and saving it as the existing record, then making a new Tag
and setting its :value to a different value, ‹"AN ARBITRARY VALUE"›,
the matcher expected the new Tag to be valid, but it was invalid
instead, producing these validation errors:
* value: ["has already been taken"]
* user: ["must exist"]
# ./spec/models/tag_spec.rb:6:in `block (3 levels) in <top (required)>'
# -e:1:in `<main>'
The correct way to test case_insensitive is to using matcher below,
it { should validate_uniqueness_of(:value).case_insensitive }
You can also write with ignoring_case_sensitivity:
it { should validate_uniqueness_of(:value).ignoring_case_sensitivity }
I have a shoulda matcher in my achievement_spec.rb and I can't get it pass:
Spec:
- require 'rails_helper'
RSpec.describe Achievement, type: :model do
describe 'validations' do
it { should validate_presence_of(:title) }
it { should validate_uniqueness_of(:title).case_insensitive.scoped_to(:user_id).with_message("you
can't have two achievements with same title")}
it { should validate_presence_of(:user) }
it { should belong_to(:user) }
end
end
Model:
- class Achievement < ActiveRecord::Base belongs_to :user
validates :title, presence: true
validates :user, presence: true
validates :title, uniqueness: {
scope: :user_id,
message: "you can't have two achievements with the same title"
}
enum privacy: [ :public_access, :private_access, :friends_access ]
def description_html
Redcarpet::Markdown.new(Redcarpet::Render::HTML).render(description)
end
end
Error:
- .F..
Failures:
1) Achievement validations should validate that :title is
case-sensitively unique within the scope of :user_id, producing a
custom validation error on failure
Failure/Error: it { should validate_uniqueness_of(:title).scoped_to(:user_id).with_message("you
can't have two achievements with same title") }
Achievement did not properly validate that :title is case-sensitively
unique within the scope of :user_id, producing a custom validation error
on failure.
After taking the given Achievement, setting its :title to ‹"an
arbitrary value"›, and saving it as the existing record, then making a
new Achievement and setting its :title to ‹"an arbitrary value"› as
well and its :user_id to a different value, ‹nil›, the matcher
expected the new Achievement to be invalid and to produce the
validation error "you can't have two achievements with same title" on
:title. The record was indeed invalid, but it produced these
validation errors instead:
* user: ["can't be blank"]
* title: ["you can't have two achievements with the same title"]
# ./spec/models/achievement_spec.rb:29:in `block (3 levels) in <top (required)>'
Finished in 0.16555 seconds (files took 3.13 seconds to load) 4
examples, 1 failure
Failed examples:
rspec ./spec/models/achievement_spec.rb:29 # Achievement validations
should validate that :title is case-sensitively unique within the
scope of :user_id, producing a custom validation error on failure
[Finished in 4.1s with exit code 1] [cmd: ['rspec', '-I
/home/mayur/Downloads/MyWork/ruby/i-rock/spec/models',
'/home/mayur/Downloads/MyWork/ruby/i-rock/spec/models/achievement_spec.rb']]
[dir: /home/mayur/Downloads/MyWork/ruby/i-rock] [path:
/usr/local/rvm/gems/ruby-2.2.4/bin:/usr/local/rvm/gems/ruby-2.2.4#global/bin:/usr/local/rvm/rubies/ruby-2.2.4/bin:/usr/lib64/qt-3.3/bin:/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/usr/local/rvm/bin:/home/mayur/.local/bin:/home/mayur/bin]
How can I get rid of above error?
I tried below solution but getting same error:
Change in model:
validates :title, uniqueness: {
case_sensitive: false,
scope: :user_id,
message: "you can't have two achievements with the same title"
}
Change in spec:
it { should validate_uniqueness_of(:title).case_insensitive.scoped_to(:user_id).with_message("you can't have two achievements with same title") }
Error Again:
.F..
Failures:
1) Achievement validations should validate that :title is case-insensitively unique within the scope of :user_id, producing a custom validation error on failure
Failure/Error: it { should validate_uniqueness_of(:title).case_insensitive.scoped_to(:user_id).with_message("you can't have two achievements with same title") }
Achievement did not properly validate that :title is case-insensitively
unique within the scope of :user_id, producing a custom validation error
on failure.
After taking the given Achievement, setting its :title to ‹"an
arbitrary value"›, and saving it as the existing record, then making a
new Achievement and setting its :title to ‹"an arbitrary value"› as
well and its :user_id to a different value, ‹nil›, the matcher
expected the new Achievement to be invalid and to produce the
validation error "you can't have two achievements with same title" on
:title. The record was indeed invalid, but it produced these
validation errors instead:
* user: ["can't be blank"]
* title: ["you can't have two achievements with the same title"]
# ./spec/models/achievement_spec.rb:29:in `block (3 levels) in <top (required)>'
Finished in 0.13566 seconds (files took 3.14 seconds to load)
4 examples, 1 failure
Failed examples:
rspec ./spec/models/achievement_spec.rb:29 # Achievement validations should validate that :title is case-insensitively unique within the scope of :user_id, producing a custom validation error on failure
The failure is occurring due to the error message defined in your spec not matching what's in your model. You're missing the word the in your spec:
Spec
... .with_message("you can't have two achievements with same title")}
Model
... message: "you can't have two achievements with the same title"
Fixing what's defined in your spec in .with_message to match what's defined in your model within the uniqueness validation message on title should resolve the failure.
You need to build a factory and ensure there is a user before running these validations:
describe Achievment do
context 'validations' do
before { FactoryGirl.build(:achievement) }
it do
should validate_uniqueness_of(:title).
scoped_to(:user_id).
case_insensitive
end
end
end
your factory then:
FactoryGirl.define do
factory :achievement
title 'some-string'
user
end
end
I'm trying to test the set_random_token method seen below from my model
class Invitation < ActiveRecord::Base
has_and_belongs_to_many :users
validates :name, presence: true, uniqueness: true
validates :number_of_uses, presence: true, numericality: true
validates :token, presence: true, uniqueness: true, format: /\A[0-9A-F]+\z/i
before_create :set_random_token
private
def set_random_token
random_hex = SecureRandom.hex(8)
self.token = random_hex
end
end
Here is the piece in my invitation_spec
it 'verifies that the token is set' do
#invitation = Invitation.new
#invitation.save
expect(#invitation.token).not_to be_empty
end
And here is the error I'm getting
2) Invitation verifies that the token is set
Failure/Error: expect(#invitation.token).not_to be_empty
expected to respond to `empty?`
# ./spec/models/invitation_spec.rb:37:in `block (2 levels) in <top (required)>'
I'm pretty new to Rails, so I apologize if the answer is extremely obvious.
I've seen times where checking the contents of an attribute/column from a local or instance variable of a record didn't yield the same expected result. The workaround I've used in the past is to query for the record with .first and check the column on that.
To ensure you are actually getting the correct record you add a expect(Table.count).to eq 0 at the beginning.
Give this a try
it 'verifies that the token is set' do
expect(Invitation.count).to eq 0
Invitation.new().save(validate: false)
expect(Invitation.count).to eq 1
expect(Invitation.first.token).not_to be_empty
end
From the Rails Documentation:
The following methods trigger validations, and will save the object to the database only if the object is valid:
save
In your test, you are instantiating an empty Invitation but Invitation has a number of validations on it which would make an empty one invalid.
Try this instead:
#invitation = Invitation.create({name: 'something unique', number_of_uses: 5})
expect(#invitation.token).not_to be_empty
#invitation.save
I have been starting to learn testing in my Rails app and am using rSpec and Shoulda.
I have the following test which works:
it { should respond_to(:park_name) }
However, what I don't understand is, what is this being run on? Is this being run on the Model itself or an instance of the model and if it's an instance of the Model then is it automatically using my Factory Girl factory?
Any simple explanations on what is actually occurring here?
UPDATE:
Ok, So I have this:
describe 'validations' do
subject { FactoryGirl.build(:coaster) }
it { should validate_presence_of(:name) }
it { should validate_presence_of(:speed) }
it { should validate_presence_of(:height) }
end
But the tests are failing. Any ideas?
Coaster.rb:
class Coaster < ActiveRecord::Base
extend FriendlyId
friendly_id :slug, use: :slugged
belongs_to :park
belongs_to :manufacturer
attr_accessible :name,
:height,
:speed,
:length,
:inversions,
:material,
:lat,
:lng,
:park_id,
:notes,
:manufacturer_id,
:style,
:covering,
:ride_style,
:model,
:layout,
:dates_ridden,
:times_ridden,
:order,
:on_ride_photo
scope :by_name_asc, lambda {
order("name ASC")
}
scope :made_from, lambda { |material|
where("material = ?", material)
}
scope :wooden, lambda {
made_from "wood"
}
scope :steel, lambda {
made_from "steel"
}
delegate :name, :location_1, :location_2, :location_3, :location_4,
to: :park,
allow_nil: true,
prefix: true
delegate :name, :url,
to: :manufacturer,
prefix: true
validates :name,
:presence => true
validates :height,
allow_nil: true,
numericality: {greater_than: 0}
validates :speed,
allow_nil: true,
numericality: {greater_than: 0}
validates :length,
allow_nil: true,
numericality: {greater_than: 0}
Test Results:
1) Coaster validations should require speed to be set
Failure/Error: it { should validate_presence_of(:speed) }
Expected errors to include "can't be blank" when speed is set to nil, got no errors
# ./spec/models/coaster_spec.rb:75:in `block (3 levels) in '
2) Coaster validations should require height to be set
Failure/Error: it { should validate_presence_of(:height) }
Expected errors to include "can't be blank" when height is set to nil, got no errors
# ./spec/models/coaster_spec.rb:76:in `block (3 levels) in '
SIMILAR QUESTION:
I have this test:
describe 'methods' do
subject { FactoryGirl.build(:coaster) }
it "should return a formatted string of coaster name at park name" do
name_and_park.should eq('Nemesis at Alton Towers')
end
end
Coaster.rb:
def name_and_park
[name, park.name].join (' at ')
end
Error when running the test:
2) Coaster methods should return a formatted string of coaster name at
park name
Failure/Error: name_and_park.should eq('Nemesis at Alton Towers')
NameError:
undefined local variable or method name_and_park' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_6:0x007f84f4161798>
# ./spec/models/coaster_spec.rb:111:inblock (3 levels) in '
It says name_and_park cannot be called but surely that method should be being called on the instance of Coaster that is being made in the subject line? No?
It's being run on "subject", which is either explicitly defined through the subject method or implicitly defined by passing in a class as the argument to describe, in which case an instance of that class is instantiated and made the subject of the test. See https://www.relishapp.com/rspec/rspec-core/v/2-0/docs/subject/explicit-subject and https://www.relishapp.com/rspec/rspec-core/v/2-0/docs/subject/implicit-subject
As for the answer to the Update question, in your model validations for :speed and :length, you have allow_nil: true, which is why those two tests are failing. Part of the definition of validates_presence_of is that nil is not a settable value.
As for your latest question, I think you may be confused about the use of implicit subjects. If should is used by itself, it will indeed default to whatever the subject is, but if you include a subject yourself, as you have in this case with name_and_park, it won't treat that as a method of the default subject, it must have a definition within the current namespace. In your case, you would need to say subject.name_and_park.should ....
On a related aside, StackOverflow is best used when you ask a specific question or related set of questions and get an answer. For a variety of reasons, it's not intended for ongoing debugging sessions. One of those reasons is that it becomes tedious tracking substantial, sequential updates of the original question and answer.