0

I'm still pretty confused about what is magic behind stuff like it { should have(1).error_on(:base) } and what's a specific Shoulda matcher.

I'd like to make sure that :base contains the error message "xxx", so how should I do this?

it "should contain error message 'xxx'" do
  contact.valid?
  contact.errors[:base].should include('xxx')
end

Is this "the way to go", or is there a better one? Thanks.

Joshua Muheim
  • 12,617
  • 9
  • 76
  • 152

1 Answers1

5

Right, it's looking good. Inline rspec tests are using subject. You could rewrite your test like this:

describe 'my method' do
  before { contact.valid? }

  context 'contact is not valid' do
    subject { contact.errors[:base] }
    it { should include 'xxx' }
  end
end

The should method is called on the subject. It can be more readable sometimes. And you don't have to write descriptions for specs that are self-explanatory ;-)

Renra
  • 5,561
  • 3
  • 15
  • 17