[rspec-users] Easy AR association stubbing

Pat Maddox pergesu at gmail.com
Sat Oct 6 18:02:55 EDT 2007


On 10/6/07, Andrew WC Brown <omen.king at gmail.com> wrote:
> But what if I need to use posts as such?
>
> @person = mock_model(Person)
> @person.stub_association!(:posts, :find_by_title => mock_model(Post))
>
> posts.stub!(:count).and_return(2)
>
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users
>

Take a look at the source: you can pass in whatever methods you want
to be stubbed.  So, for example,

@person.stub_association!(:posts, :find_by_title => mock_model(Post),
:count => 2)

Also, you should be able to stub after the fact like so:

@person.posts.stub!(:count).and_return 2

Pat


More information about the rspec-users mailing list