[rspec-devel] Fwd: [ rspec-Bugs-6905 ] Mocking rails associations where the association is validated

Pat Maddox pergesu at gmail.com
Mon Nov 27 04:54:53 EST 2006

Another question :)

When I first wrote the add_person method, my spec looked like this:

  specify "should accept a new person" do
    @user.people.size.should_be 0
    @user.add_person Person.new
    @user.people.size.should_be 1

That works, of course, and lets me infer that I have the new user.
However I think it's ugly that I'm accessing people to verify
something about the user...

So then I decided to change it to this:

  specify "should accept a new person" do
    mock_person = mock("person")
    mock_people = mock("people")
    @user.add_person mock_person

That feels like the more RSpecy way to do this, despite being longer.
In both cases they dig into people (either checking the size or
checking that it calls <<), but in the latter case it verifies
behavior, rather than inferring it from state.  Does that sound right?


More information about the rspec-devel mailing list