Im trying a challenge where I need to do some validations. I should pass on the following tests:
it 'should not allow political words' do
body = "Petralha Tucanos Dilma"
opinion = Opinion.new(body: body)
expect(opinion).to_not be_valid
expect(opinion.errors[:body]).to include "Contains political words"
end
it 'is invalid when has no body' do
body = ''
opinion = Opinion.new(body: body)
expect(opinion).to_not be_valid
expect(opinion.errors[:body]).to include 'can\'t be blank'
end
it 'is invalid when has more than 150 caracters' do
body = 'a' * 151
opinion = Opinion.new(body: body)
expect(opinion).to_not be_valid
expect(opinion.errors[:body]).to include 'is too long'
end
To do so I tried the following on my model:
class Opinion < ActiveRecord::Base
validates :body, presence: true, length: { maximum: 150 }, exclusion: { in: %w(Petralha Tucanos Dilma) }
end
Im still a noob on Rails, so anyone have a clue? Those are the errors I get:
1) Opinion should not allow political words Failure/Error: expect(opinion).to_not be_valid expected # not to be valid # ./spec/models/opinion_spec.rb:16:in `block (2 levels) in '
2) Opinion is invalid when has more than 150 caracters Failure/Error: expect(opinion.errors[:body]).to include 'is too long' expected ["is too long (maximum is 150 characters)"] to include "is too long" # ./spec/models/opinion_spec.rb:33:in `block (2 levels) in '
Finished in 0.12861 seconds (files took 0.64205 seconds to load) 11 examples, 2 failures
Failed examples:
rspec ./spec/models/opinion_spec.rb:12 # Opinion should not allow political words rspec ./spec/models/opinion_spec.rb:28 # Opinion is invalid when has more than 150 caracters