I'm hitting a wall because I'm terribly confused about what this rspec error message means. I am testing to see whether an art piece has a cost. Here are snippets of my rspec:
let(:valid_art_piece){ { date_of_creation: DateTime.parse('2012-3-13'),
placement_date_of_sale: DateTime.parse('2014-8-13'), cost: 250, medium: 'sculpture',
availability: true } }
it 'requires a cost' do
art_piece = ArtPiece.new(valid_art_piece.merge(cost: ''))
expect(art_piece).to_not be_valid
expect(art_piece.errors[:cost].to include "can't be blank")
end
error message:
1) ArtPiece requires a cost
Failure/Error: expect(art_piece.errors[:cost].to include "can't be blank")
NoMethodError:
undefined method `+' for #<RSpec::Matchers::BuiltIn::Include:0x000001050f4cd0>
# ./spec/models/artist_piece_spec.rb:30:in `block (2 levels) in <top (required)>'
As far as I'm concerned this shouldn't have failed and I don't know why its failing. My schema.rb has the field as null false and my model validates for it with the numericality: true option.
class ArtPiece < ActiveRecord::Base
validates_presence_of :date_of_creation
validates_presence_of :placement_date_of_sale
validates :cost, numericality: true
validates_presence_of :medium
validates_presence_of :availability
end
I don't know what the problem is. Some help?
It's an syntax error. You missed the brackets before .to
and before "can't be blank"
:
This line should looks like that:
expect(art_piece.errors[:cost] ).to include ("can't be blank")
Anyway, I suggest to replace include
method using match_array
it 'requires a cost' do
art_piece = ArtPiece.new(valid_art_piece.merge(cost: ''))
expect(art_piece).to_not be_valid
expect(art_piece.errors[:cost]).to match_array (["can't be blank"])
end
PS: It is good conventionto implement only one expectation per test.
Final example should looks like that:
context "art_piece" do
subject { ArtPiece.new(valid_art_piece.merge(cost: '')) }
it 'should_not be valid' do
expect(subject).to_not be_valid
end
it 'requires a cost' do
expect(subject.errors[:cost]).to match_array (["can't be blank"])
end
end
hope it will help :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With