Shoulda/RSpec matchers - conditional validation
It doesn't appear that shoulda_matchers does this, but it's easy enough to write it yourself::
context "if eligible" do
before { allow(subject).to receive(:eligible?).and_return(true) }
it { should validate_presence_of(:name) }
end
context "if ineligible" do
before { allow(subject).to receive(:eligible?).and_return(false) }
it { should_not validate_presence_of(:name) }
end
How to test conditional validation with rspec-rails 3.0.0 and shoulda-matchers 2.5.0
Yeah, unfortunately there isn't another way. The fact is that time
is technically allowed to be nil before you save your Event record, so if you happen to call #happened? before time
gets set, it will raise an exception. So if I wrote this code I would personally be okay with placing a check in #happened?, because it's a case you would need to handle anyway (regardless of the chance this will actually happen).
How to use Shoulda matchers to test date validation
First, you should use a proc for the evaluation of the dates
validates :return_date, inclusion: { in: ->(model) { (Date.today+1.day)..(Date.today+4.weeks) } }
Then yon can make adjustments to the given field to see when it is false and true
# Set the expectation that the factory is setup correctly
expect(quotation_request).to be_valid
# Then check that values "just" out of range make it invalid
quotation_request.return_date = Date.today
expect(quotation_request).not_to be_valid
quotation_request.return_date = Date.today+4.weeks+1.day
expect(quotation_request).not_to be_valid
Shoulda matcher `validate_length_of` in RSpec with Reform form
Reform is known not to work with shoulda-matchers. Basically, all of the model matchers in shoulda-matchers work by:
- Making a new instance of the class under test.
- Setting attributes on that instance.
- Calling
validate
on that instance. - Reading any possible validation errors from
errors
.
However, Reform doesn't work this way; you don't set properties on the form object individually and then call valid?
, you call validate
with the properties you want to set on the form object, and then those properties are validated in the process of being set. So that's likely why you're getting this error — because shoulda-matchers is trying to manually set the properties, and then validate
is blowing them away.
Unfortunately, there's not a way to get shoulda-matchers to work with Reform without writing some kind of adapter. We don't plan on adding this to the gem any time soon but we will take a PR! Other than this, apparently the Reform team was talking about making some RSpec matchers in the spirit of shoulda-matchers, but I'm not sure how much progress has been made there, if any.
Sorry I couldn't be more helpful :/
Rspec shoulda matcher test on activerecord model fails due to validation of associated model
Setting
subject { create :new_offer_range }
before it
-blocks should solve the problem. It will set the subject of testing, not a default object, but one that you want to have.
Ruby on Rails with RSpec and shoulda-matcher validate_inclusion_of giving NoMethodError
I have figured it out. For some reason the validate_inclusion_of works when it is within a let! block.
It doesn't matter what argument is sent to the let helper method, just that one is.
Still don't know WHY this fixes it though, if anybody can enlighten me!?
So, the following spec now passes
describe 'company_user_group list of valid IDs' do
let!(:company_user_group_everything) { FactoryGirl.create(:company_user_group_everything) }
let!(:company_user_group_nothing) { FactoryGirl.create(:company_user_group_nothing) }
let!(:temp) do
it {is_expected.to validate_inclusion_of(:company_user_group_id).in_array(CompanyUserGroup.full_list_of_ids)}
end
end
How can I use a custom validation with shoulda matchers?
Your problem is that your validation is not expecting value
to be nil. Change your method to:
class PiecesValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
unless value && value > 0 && value <= Puzzle.remaining
record.errors[attribute] << (options[:message] || "Puzzle pieces must be between 1 and #{Puzzle.remaining}")
end
end
end
This will not add an error if validated field is blank. However, what you are trying to do can be achieved using standard rails validators:
validates :puzzle_pieces, numericality: { only_integer: true, less_then: Puzzle.remaining, greater_then: 0 }
Related Topics
How to Make the Say Command Echo a Variable Value in a Script
How to Make the Days of the Month Be Printed According to Each Day (Ex: Su Mo Tu We...Etc)
How to List All Databases with Activerecord
Heroku: Push Rejected, Failed to Compile Ruby App
Placing Sub Arrays in Neat Rows
Newbie: Text Replacement in My Case
What Does the |Variable| Syntax Mean
Mail_Form Gem with Sidekiq Worker
Ruby on Rails Updating Heroku Dynamic Routes
Jekyll Custom Theme- Gemspec Bundle Install Error: Unexpected Unary-, Expecting Keyword_Do
How to Use Local or Instance Variable in Ruby Code in Coffeescript in Haml Template
Why Does 'Defined' Return a String or Nil
How to Extract Specific Elements from an Array
Can't Find Ffi.H When Installing Ffi Ruby Gem
Ruby on Rails - Error Making New App (Rb:55:In 'Require')
Rexml::Document.New How to Give Encode Parameters on This Line