15. model validations
it { should be_invalid }
its(:errors) { should be_present }
it { should have(1).errors_on(:quantity) }
16. shoulda-matchers
it 'should validate email and quantity' do
subject.email = 'test@example.com'
subject.quantity = -1
subject.valid?
should have(:no).errors_on(:email)
should have_at_least(1).error_on(:quantity)
end
# vs.
it { should allow_value('test@example.com').for(:email) }
it { should_not allow_value(-1).for(:quantity) }
18. rails model mocks
mock_model is a test double that acts like an
ActiveModel
stub_model is a instance of a real
ActiveModel with some methods stubbed
19. using mocks
let(:email_template) do
stub_model(EmailTemplate,
:name => 'test_email', ...)
end
before do
...
EmailTemplate.stub(:find_by_name).
with('test_email').
and_return(email_template)
end