1

I have trouble with testing model count through feature/scenario rspec instrument

require 'spec_helper'

feature 'Registration' do
  scenario 'Guest can sign up as individual' do
    with_role(:guest)
    sign_up(:client)
    Individual.count.should == 1
    # should be replaced by expect{ sign_up(:client) }.to change{ Individual.count }.by(1)
  end
end

How can I replace model.count should to the expect in the block to expecting in scenario? I don't want to use should because of record count changes every time I use it

Regards, Alex

3
  • What are you asking for? What's wrong with that expect? Commented Dec 12, 2013 at 13:09
  • Why would calling should change the count? Commented Dec 12, 2013 at 14:48
  • phoet, it's don't work Commented Dec 13, 2013 at 1:07

1 Answer 1

1

The correct way of using the change matcher of rspec is as follows:

expect{ sign_up(:client) }.to change(Individual, :count).by(1)
Sign up to request clarification or add additional context in comments.

2 Comments

Hm. I use this now, but it seems little complicated to use expect in scenario, isn't it?
Well, I really don't think so. It's much more expressive to document and reveal the intention of the spec. It is almost like fluent english. I like this form.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.