Last Updated: February 25, 2016
· orieken

Testing your AR models made easy with accepts_values_for

I was looking for an easy way to test my models and I came across this gem

an example taken from here (

it "should be required" do
  blank =, :name => "")
  blank.should_not be_valid
  blank.errors[:name].should include("can't be blank") = "Foo"
  blank.should be_valid

but this looks a lot cleaner and does the same

it { should accept_values_for(:name, "Foo") }
it { should_not accept_values_for(:name, nil) }

2 Responses
Add your response

So much cleaner, thanks for sharing!

over 1 year ago ·

What about using shoulda matchers? They have a matcher which does essentially the same (validate an attribute can't be blank).

it { should validate_presence_of(:name) }

You could combine this with a Factory which has all default values and test if this Factory is valid:

FactoryGirl.define do
  factory :my_model do
    name "Foo"

let(:my_model) { FactoryGirl.create(:my_model) }
it { my_model.should be_valid }
over 1 year ago ·