I have the following model :
class Court < ActiveRecord::Base
#Relationships
#belongs_to :case, class_name: 'Case', foreign_key: 'case_id'
belongs_to :user, class_name: 'User', foreign_key: 'user_id'
#Scopes
#Attributes
attr_accessible :court_name, :court_notes, :street, :city, :state, :zip
#Validations
validates_lengths_from_database
validates :court_name, presence: true, length: { in: 3..200 }
validates :court_notes, length: { maximum: 250 }
validates :court_notes, :street, :city, :state, :zip, presence: true
validates :street, :city, :state, length: { maximum: 30, message: 'max length allowed is 30' }
validates :zip, numericality: true, length: { is: 5, message: 'length should be 5' }, allow_blank: true
#Callbacks
#Methods
end
And the following spec file :
require 'spec_helper'
describe Court do
context '#object' do
it 'has a valid factory' do
FactoryGirl.build(:court).should be_valid
end
end
context '#associations' do
it { should belong_to(:user) }
end
context '#values' do
it { should respond_to(:court_name) }
it { should respond_to(:court_notes) }
it { should respond_to(:street) }
it { should respond_to(:city) }
it { should respond_to(:state) }
it { should respond_to(:zip) }
end
context '#protected' do
it { should_not allow_mass_assignment_of(:id) }
it { should_not allow_mass_assignment_of(:case_id) }
end
context '#validations' do
it { should validate_presence_of(:court_name) }
it { should ensure_length_of(:court_name).is_at_most(200) }
it { should ensure_length_of(:court_notes).is_at_most(250) }
it { should ensure_length_of(:street).is_at_most(30) }
it { should ensure_length_of(:city).is_at_most(30) }
it { should ensure_length_of(:state).is_at_most(30) }
end
end
When I run the spec, I get the error
1) Court#validations
Failure/Error: it { should ensure_length_of(:city).is_at_most(30) }
Did not expect errors to include "is too long (maximum is 30 characters)" when city is set to "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", got error:
# ./spec/models/court_spec.rb:52:in `block (3 levels) in <top (required)>'
and two other similar errors for city and state. Where is the extra error message being added from? The validation is happening at only one place as far as I know but there are two error messages being produced.
The reason is your custom message:
message: 'max length allowed is 30'
Shoulda expects you have exact error message as default:
'is too long (maximum is 30 characters)'
But you have a different message so expectation fails. You can check Shoula doc to see how to allow custom message.
Related
I wanted to see if the validation was working properly, so I wrote a test of the model in Rspec.
models/work_history.rb
class WorkHistory < ApplicationRecord
belongs_to :account
validates :name,
:since_date,
:position, presence: true
validates :is_employed, inclusion: [true, false]
validates :until_date, presence: true, if: -> { :is_employed == false }
end
factories/work_histories.rb
FactoryBot.define do
factory :work_history do
sequence(:name) { |n| "#{n}_company" }
since_date { '2017-04-01' }
until_date { '2021-03-01' }
is_employed { false }
position { 'director' }
department { 'sales' }
association :account
end
end
spec/models/work_history_spec.rb
RSpec.describe WorkHistory, type: :model do
let(:account) { create(:account) }
let(:work_history) { build(:work_history, account: account) }
it "is invalid when is_employed is false and until_date is nil" do
work_history.is_employed = false
work_history.until_date = ''
expect(work_history.valid?).to eq(false)
end
it "is invalid when is_employed is employed" do
work_history.is_employed = 'employed'
expect(work_history.valid?).to eq(false)
end
end
then I run rspec command, but the test did't pass.
this is the rspec test error log
docker-compose run app rspec spec/models
Creating toei-works_app_run ... done
FF
Failures:
1) WorkHistory is invalid when is_employed is false and until_date is nil
Failure/Error: expect(work_history.valid?).to eq(false)
expected: false
got: true
(compared using ==)
Diff:
## -1 +1 ##
-false
+true
# ./spec/models/work_history_spec.rb:10:in `block (2 levels) in <top (required)>'
2) WorkHistory is invalid when is_employed is employed
Failure/Error: expect(work_history.valid?).to eq(false)
expected: false
got: true
(compared using ==)
Diff:
## -1 +1 ##
-false
+true
# ./spec/models/work_history_spec.rb:15:in `block (2 levels) in <top (required)>'
I don't know why Rspec tests fail
Why can't I pass rspec's test?
You're not structuring the if part of your validation correctly. You're currently checking whether the symbol :is_employed? equals false, which it never will. You can check out the correct syntax here:
https://guides.rubyonrails.org/active_record_validations.html#conditional-validation
You can either do
class WorkHistory < ApplicationRecord
belongs_to :account
validates :name,
:since_date,
:position, presence: true
validates :is_employed, inclusion: [true, false]
validates :until_date, presence: true, if: -> { :not_employed? }
private
def not_enployed?
!is_employed?
end
end
or
class WorkHistory < ApplicationRecord
belongs_to :account
validates :name,
:since_date,
:position, presence: true
validates :is_employed, inclusion: [true, false]
validates :until_date, presence: true, unless: Proc.new { |work_history| work_history.is_employed? }
end
I'm using Rails 4 enums and I want to properly test them, so I set these tests up for my enum fields:
it { should validate_inclusion_of(:category).in_array(%w[sale sale_with_tax fees lease tax_free other payroll]) }
it { should validate_inclusion_of(:type).in_array(%w[receivable payable]) }
And this is the model they're validating:
class Invoice < ActiveRecord::Base
belongs_to :user
enum category: [:sale, :sale_with_tax, :fees, :lease, :tax_free, :other, :payroll]
enum type: [:receivable, :payable]
validates :user, presence: true
validates :issue_date, presence: true
validates :series, presence: true
validates :folio, presence: true
validates :issuing_location, presence: true
validates :payment_method, presence: true
validates :last_digits, presence: true
validates :credit_note, presence: true
validates :total, presence: true
validates :subtotal, presence: true
validates :category, presence: true
validates_inclusion_of :category, in: Invoice.categories.keys
validates :type, presence: true
validates_inclusion_of :type, in: Invoice.types.keys
end
But when I run the tests I get:
1) Invoice should ensure inclusion of type in [0, 1]
Failure/Error: it { should validate_inclusion_of(:type).in_array([0,1]) }
ArgumentError:
'123456789' is not a valid type
# ./spec/models/invoice_spec.rb:20:in `block (2 levels) in <top (required)>'
2) Invoice should ensure inclusion of category in [0, 1, 2, 3, 4, 5, 6]
Failure/Error: it { should validate_inclusion_of(:category).in_array([0,1,2,3,4,5,6]) }
ArgumentError:
'123456789' is not a valid category
# ./spec/models/invoice_spec.rb:19:in `block (2 levels) in <top (required)>'
I've also tried with string values in the test arrays, but I get the same error and I really don't understand it.
Using Shoulda matchers we can use the following to test the enum
it { should define_enum_for(:type).with([:receivable, :payable]) }
it { should define_enum_for(:category).
with(:sale, :sale_with_tax, :fees, :lease, :tax_free, :other, :payroll) }
Try this:
it { should validate_inclusion_of(:category).in_array(%w[sale sale_with_tax fees lease tax_free other payroll].map(&:to_sym)) }
Additionally, for code-cleanup, try putting the valid categories/types in a corresponding constant. Example:
class Invoice < ActiveRecord::Base
INVOICE_CATEGORIES = [:sale, :sale_with_tax, :fees, :lease, :tax_free, :other, :payroll]
enum category: INVOICE_CATEGORIES
end
Your migration could be the issue, it should look something like:
t.integer :type, default: 1
You may also consider testing this another way.
Maybe more like:
it "validates the category" do
expect(invoice with category fee).to be_valid
end
Use shoulda matchers along with check for column_type.
it do
should define_enum_for(:type).
with_values(:receivable, :payable).
backed_by_column_of_type(:integer)
end
it do
should define_enum_for(:category).
with_values(:sale, :sale_with_tax, :fees, :lease, :tax_free, :other, :payroll).
backed_by_column_of_type(:integer)
end
Just use shoulda matchers:
it { should define_enum_for(:type).with_values([:receivable, :payable]) }
it { should define_enum_for(:category).with_values(:sale, :sale_with_tax, :fees, :lease, :tax_free, :other, :payroll)}
You have this string in your validations:
validates_inclusion_of :category, in: Invoice.categories.keys
In case of enum
Invoice.categories.keys #=> ["sale", "sale_with_tax", "fees", "lease", "tax_free", "other", "payroll"]
You should update your object data with one of names of your enum.
model
class RecipeIngredient < ActiveRecord::Base
validates :recipe_id, presence: true, on: :save
validates :ingredient_id, presence: true, on: :save
belongs_to :recipe
belongs_to :ingredient
has_many :quantities
end
test
require 'spec_helper'
describe RecipeIngredient do
it { should validate_presence_of(:recipe_id) }
it { should validate_presence_of(:ingredient_id) }
it { should belong_to(:recipe) }
it { should belong_to(:ingredient) }
context "valid" do
subject { RecipeIngredient.new(recipe_id: 1, ingredient_id: 1) }
it { should be_valid }
end
end
returns
Failures:
1) RecipeIngredient
Failure/Error: it { should validate_presence_of(:recipe_id) }
Did not expect errors to include "can't be blank" when recipe_id is set to nil, got error:
# ./spec/models/recipe_ingredient_spec.rb:4:in `block (2 levels) in '
2) RecipeIngredient
Failure/Error: it { should validate_presence_of(:ingredient_id) }
Did not expect errors to include "can't be blank" when ingredient_id is set to nil, got error:
# ./spec/models/recipe_ingredient_spec.rb:5:in `block (2 levels) in '
I don't understand why adding on: :save has broken this test
First off, you don't need to put the on: :save clause. That is the default and can be left off.
Following the Michael Hartl rails tutorial. Struggling to get the remember token tests to pass, specifically, the test for non-blankness of the remember token.
The code for the User class is below
class User < ActiveRecord::Base
attr_accessible :name, :email, :password, :password_confirmation
has_secure_password
before_save { |user| user.email = email.downcase }
before_save { :create_remember_token }
validates :name, presence: true, length: { maximum: 50 }
VALID_EMAIL_REGEX = /\A[\w+\-.]+#[a-z\d\-.]+\.[a-z]+\z/i
validates :email, presence: true, format: { with: VALID_EMAIL_REGEX }, uniqueness: {case_sensitive: false}
validates :password, presence: true, length: { minimum: 6 }
validates :password_confirmation, presence: true
private
def create_remember_token
self.remember_token = SecureRandom.urlsafe_base64
end
end
and the test I can't get to pass:
subject { #user }
...
describe "remember token" do
before { #user.save }
its (:remember_token) { should_not be_blank }
end
and the error message I get is:
.....................F
Failures:
1) User remember token remember_token
Failure/Error: its (:remember_token) { should_not be_blank }
expected blank? to return false, got true
# ./spec/models/user_spec.rb:120:in `block (3 levels) in <top (required)>'
Finished in 0.68878 seconds
22 examples, 1 failure
Failed examples:
rspec ./spec/models/user_spec.rb:120 # User remember token remember_token
I don't know if this is relevant but sublime_text isn't doing anything with the self keyword (ie its not highlighting it in a different colour).
I'm using Ruby 1.9.3
The issue is that you're writing before_save { :create_remember_token } when you should have before_save :create_remember_token. The { } is a block. Same as when you do
do
#This is some code
end
That is also a block of code.
That's why your first before_save works, because you're giving the block a piece of code to execute. In the second before_save you're just giving it the name of the method to execute which has the block of code.
Tl:dr:
Change
before_save { :create_remember_token }
to
before_save :create_remember_token
and you should be good to go.
I worked through all of Michael Hartl's Ruby on Rails Tutorial with all the tests passing. Now that I'm going back and making changes to the site to suit my own needs, it's not as cut and dry as "the tests in this section aren't passing." I've created a new "Charity" object that is strongly based on Hartl's "Micropost" object. The only difference is that instead of having "content" the object has a :name, :description and :summary.
This is the code for the test that is failing, (specifically "it { should be_valid }") which is located in /charity_spec.rb:
require 'spec_helper'
describe Charity do
let(:user) { FactoryGirl.create(:user) }
before { #charity = user.charities.build(summary: "Lorem ipsum") }
subject { #charity }
it { should respond_to(:name) }
it { should respond_to(:user_id) }
it { should respond_to(:summary) }
it { should respond_to(:description) }
it { should respond_to(:user) }
its(:user) { should == user }
it { should be_valid }
...
The test actually passes at first, but once I add the validations to the charity.rb file, they return;
Failures:
1) Charity
Failure/Error: it { should be_valid }
expected valid? to return, true, got false
...
Here's the charity.rb:
class Charity < ActiveRecord::Base
attr_accessible :name, :description, :summary
belongs_to :user
validates :name, presence: true, length: { maximum: 40 }
validates :summary, presence: true
validates :description, presence: true
validates :user_id, presence: true
default_scope order: 'charities.created_at DESC'
end
I'm sure it's something stupid, but my understanding of everything is so weak I can't figure out what I'm doing wrong, my feeling is that it's something wrong with my factory, but I really don't know.
Here's my charity factory located in the factories.rb:
factory :charity do
name "Lorem ipsum"
summary "Lorem ipsum"
description "Lorem ipsum"
user
end
When I remove the :name, :summary, and :description validations from charity.rb, the test passes. For good measure, here's the beginning of my user.rb:
class User < ActiveRecord::Base
attr_accessible :name, :email, :password, :password_confirmation
has_secure_password
has_many :charities
has_many :microposts, dependent: :destroy
Use your factory to have a proper charity:
before { #charity = user.charities.build(FactoryGirl.attributes_for(:charity)) }
It failed because you validate presence of attributes which were not set like name
If you need more background on FactoryGirl, their documentation is really good.