I''m having a terrible time trying to test a nested route with rSpec.
I can''t seem to tell rSpec to expect the call @item.user. The
following error message plagues me not matter how I try to fuss with
the mock:
Spec::Mocks::MockExpectationError in ''ItemsController handling POST /
items should redirect to the new course on successful save''
Mock ''Item_1002'' received unexpected message :user= with
(#<User:
0x..fdb7bcd38 @name="User_1003">)
Any help would be greatly appreciated.
Here''s my setup. Problem line indicated with ******
--routes.rb--
map.resources :users do |users|
users.resources :items
end
--items_controller--
def create
@item = Item.new(params[:item])
@item.user = User.find(params[:user_id])
respond_to do |format|
if @item.save
....etc.....
--items_controller_spec--
describe ItemsController, "create action" do
before do
@item = mock_model(Item, :to_param => "1")
@user = mock_model(User, :to_param => "1")
Item.stub!(:new).and_return(@course)
User.stub!(:find).and_return(@user)
end
def post_with_successful_save
***** @item.should_receive(:user)
@item.should_receive(:save).and_return(true)
post :create, :user_id => 1, :item => {}
end
it "should redirect to the new course on successful save" do
post_with_successful_save
response.should redirect_to(item_url("1"))
end
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
On 9/18/07, knb <kylebanker-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > I''m having a terrible time trying to test a nested route with rSpec. > I can''t seem to tell rSpec to expect the call @item.user. The > following error message plagues me not matter how I try to fuss with > the mock: > > Spec::Mocks::MockExpectationError in ''ItemsController handling POST / > items should redirect to the new course on successful save'' > Mock ''Item_1002'' received unexpected message :user= with (#<User: > 0x..fdb7bcd38 @name="User_1003">) > > Any help would be greatly appreciated. > > > Here''s my setup. Problem line indicated with ****** > > --routes.rb-- > map.resources :users do |users| > users.resources :items > end > > --items_controller-- > def create > @item = Item.new(params[:item]) > @item.user = User.find(params[:user_id]) > > respond_to do |format| > if @item.save > ....etc..... > > --items_controller_spec-- > describe ItemsController, "create action" do > > before do > @item = mock_model(Item, :to_param => "1") > @user = mock_model(User, :to_param => "1") > Item.stub!(:new).and_return(@course) > User.stub!(:find).and_return(@user) > end > > def post_with_successful_save > ***** @item.should_receive(:user) > @item.should_receive(:save).and_return(true) > post :create, :user_id => 1, :item => {} > end > > it "should redirect to the new course on successful save" do > post_with_successful_save > response.should redirect_to(item_url("1")) > end > > > > >The problem is that you''re expecting #user when you''re actually calling #user=. So your expectation should be @item.should_receive(:user=).with(@user) and you''ll also want to stub the call in your before block. @item = mock_model(Item, :to_param => "1", :user= true) Pat --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 9/18/07, Pat Maddox <pergesu-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 9/18/07, knb <kylebanker-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > I''m having a terrible time trying to test a nested route with rSpec. > > I can''t seem to tell rSpec to expect the call @item.user. The > > following error message plagues me not matter how I try to fuss with > > the mock: > > > > Spec::Mocks::MockExpectationError in ''ItemsController handling POST / > > items should redirect to the new course on successful save'' > > Mock ''Item_1002'' received unexpected message :user= with (#<User: > > 0x..fdb7bcd38 @name="User_1003">) > > > > Any help would be greatly appreciated. > > > > > > Here''s my setup. Problem line indicated with ****** > > > > --routes.rb-- > > map.resources :users do |users| > > users.resources :items > > end > > > > --items_controller-- > > def create > > @item = Item.new(params[:item]) > > @item.user = User.find(params[:user_id]) > > > > respond_to do |format| > > if @item.save > > ....etc..... > > > > --items_controller_spec-- > > describe ItemsController, "create action" do > > > > before do > > @item = mock_model(Item, :to_param => "1") > > @user = mock_model(User, :to_param => "1") > > Item.stub!(:new).and_return(@course) > > User.stub!(:find).and_return(@user) > > end > > > > def post_with_successful_save > > ***** @item.should_receive(:user) > > @item.should_receive(:save).and_return(true) > > post :create, :user_id => 1, :item => {} > > end > > > > it "should redirect to the new course on successful save" do > > post_with_successful_save > > response.should redirect_to(item_url("1")) > > end > > > > > > > > > > > > The problem is that you''re expecting #user when you''re actually > calling #user=. So your expectation should be > > @item.should_receive(:user=).with(@user) > > and you''ll also want to stub the call in your before block. > @item = mock_model(Item, :to_param => "1", :user= true) > > Pat >erm, you don''t need to stub it since you''re setting the expectation every time in post_with_successful_save. Pat --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---