Skip to content

Instantly share code, notes, and snippets.

@joel
Created July 3, 2012 06:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joel/3038168 to your computer and use it in GitHub Desktop.
Save joel/3038168 to your computer and use it in GitHub Desktop.
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