Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Rspec Custom Matcher require_presence_of
# Matcher place into spec/support/require_presence_of.rb
RSpec::Matchers.define :require_presence_of do |attr|
match do |model|
model.send("#{attr}=", nil)
!model.valid? && model.errors.has_key?(attr)
end
end
# Sample of use
require 'spec_helper'
describe MyModel do
let(:my_model) { Factory.build(:my_model) }
subject { my_model }
it "should_not accept nil price" do
should require_presence_of(:price)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment