Hi friends in my rspec_controller 1]@user.articles.should_receive(:find_by_id).with(''1'').and_return(@article) get :show,:id => 1 in above example 1 if i mention get :show, :id => 1 below the schould_receive it is working fine but 2]get :show,:id => 1 @user.articles.should_receive(:find_by_id).with(''1'').and_return(@article) if i mention in the above way it is raising error! Why ? What is the reason? Could any one explain please ? Thanks in advance? -- Posted via http://www.ruby-forum.com/.
David Chelimsky
2010-Nov-16 06:12 UTC
[rspec-users] why should_receive behaves differently
On Nov 15, 2010, at 10:31 PM, Sai Babu wrote:> Hi friends > > in my rspec_controller > > 1]@user.articles.should_receive(:find_by_id).with(''1'').and_return(@article) > > get :show,:id => 1 > > in above example 1 if i mention get :show, :id => 1 below the > schould_receive > it is working fine > > but > 2]get :show,:id => 1 > @user.articles.should_receive(:find_by_id).with(''1'').and_return(@article) > > if i mention in the above way it is raising error! > > Why ? > What is the reason? > Could any one explain please ? > Thanks in advance?When you say "foo.should_receive(:bar)" you''re saying "foo should receive bar sometime between now and the end of this example. The first example, the "get" is doing something that causes @user.articles to receive :find_by_id, but in the second example that''s already happened before you set the expectation and does not happen again, so you get a failure message. HTH, David
Oh! i understood now Thanks for good explanation David Chelimsky wrote in post #961764:> On Nov 15, 2010, at 10:31 PM, Sai Babu wrote: > >> it is working fine >> Thanks in advance? > When you say "foo.should_receive(:bar)" you''re saying "foo should > receive bar sometime between now and the end of this example. The first > example, the "get" is doing something that causes @user.articles to > receive :find_by_id, but in the second example that''s already happened > before you set the expectation and does not happen again, so you get a > failure message. > > HTH, > David-- Posted via http://www.ruby-forum.com/.