Aaron Renoir
Aaron Renoir

Reputation: 4381

testing strict validations in rails 3.2 with rspec

In rspec how do I test what attribute failed the strict validation. I have only been able to test if the "ActiveModel::StrictValidationFailed" exception was thrown.

Here is a example:

it "should not be valid if the asset already exists" do
     n = Factory.build( :private_attached_asset, :asset => Rack::Test::UploadedFile.new( "test.pdf", 'application/pdf' ))
     expect { n.save }.should raise_error(ActiveModel::StrictValidationFailed)
     #n.should have(1).error_on(:checksum)
 end

The commented out line throws the exception again.

Upvotes: 0

Views: 429

Answers (1)

Tanzeeb Khalili
Tanzeeb Khalili

Reputation: 7344

You can't check for error messages on strict validations because they raise immediately and don't set the errors object. Alternatively, you can test the exact error message raised:

expect { n.valid? }.to raise_error(ActiveModel::StrictValidationFailed, 'Exact message thrown')

Upvotes: 1

Related Questions