Fischer, Daniel
2007-Jul-12 08:21 UTC
[rspec-users] Agh, this is annoying. Why is this happening?
My problem: Mock ''Task_1005'' received unexpected message :user_id= with (1) No matter what I do to try to stub that out it will still fail out and give me that message. Here is my spec describe TasksController, "handling POST /tasks" do before(:each) do @task = mock_model(Task, :to_param => "1", :save => true) Task.stub!(:new).and_return(@task) @user = mock_model(User) @user.stub!(:id).and_return(1) @user.stub!(:login).and_return("moo") User.stub!(:find).and_return(@user) @params = {} end def do_post @request.session[:user] = @user.id post :create, :task => @params end it "should create a new task" do Task.should_receive(:user_id).with(@user.id).and_return(true) Task.should_receive(:new).with(@params).and_return(@task) do_post end it "should redirect to /tasks" do Task.should_receive(:user_id).with(@user.id).and_return(true) do_post response.should redirect_to(home_url) end end And my controller: def create @task = Task.new(params[:task]) @task.user_id = current_user.id @task.status = Status.find_by_name(''in progress'') respond_to do |format| if @task.save flash[:notice] = ''Task was successfully created.'' format.html { redirect_to home_url } format.xml { head :created, :location => task_url(@task) } else flash[:error] = @task.errors format.html { redirect_to home_url } format.xml { render :xml => @task.errors.to_xml } end end end So obviously it''s failing with the current_user.id thing. I''m using restful_authentication. Thanks! -------------- next part -------------- An HTML attachment was scrubbed... URL: http://rubyforge.org/pipermail/rspec-users/attachments/20070712/266c9c6d/attachment-0001.html
Ashley Moran
2007-Jul-12 09:06 UTC
[rspec-users] Agh, this is annoying. Why is this happening?
On 12 Jul 2007, at 09:21, Fischer, Daniel wrote:> My problem: > > Mock ''Task_1005'' received unexpected message :user_id= with (1) > > No matter what I do to try to stub that out it will still fail out > and give me that message. > > Here is my spec > > > describe TasksController, "handling POST /tasks" do > before(:each) do > @task = mock_model(Task, :to_param => "1", :save => true)@task = mock_model(Task, :to_param => "1", :save => true, :user_id= => nil)> > Task.stub!(:new).and_return(@task) > @user = mock_model(User) > @user.stub!(:id).and_return(1) > @user.stub!(:login).and_return("moo") > User.stub!(:find).and_return(@user) > @params = {} > end > > def do_post > @request.session[:user] = @ user.id > post :create, :task => @params > end > > it "should create a new task" do > Task.should_receive(:user_id).with(@user.id).and_return(true)@task.should_receive(:user_id=).with(@user.id).and_return(true)> > Task.should_receive(:new).with(@params).and_return(@task) > do_post > end > > it "should redirect to /tasks" do > Task.should_receive(:user_id).with(@user.id ).and_return(true) > do_post > response.should redirect_to(home_url) > end > end > > And my controller: > > def create > @task = Task.new(params[:task]) > @task.user_id = current_user.id > @task.status = Status.find_by_name(''in progress'') > > respond_to do |format| > if @task.save > flash[:notice] = ''Task was successfully created.'' > format.html { redirect_to home_url } > format.xml { head :created, :location => task_url(@task) } > else > flash[:error] = @task.errors > format.html { redirect_to home_url } > format.xml { render :xml => @ task.errors.to_xml } > end > end > end > > So obviously it''s failing with the current_user.id thing. I''m using > restful_authentication.Daniel, It looks like it''s failing for a different reason than you think, if I understood your code. (Although I only woke up an hour ago and by rights I should still be in bed...) I''ve never used restful_authentication, so I don''t know about "current_user", but I suspect it needs to be stubbed out - otherwise you are running some sort of semi-integration test, testing the plugin as well as your own code. Also, like David pointed out yesterday, you probably shouldn''t combine specs. You want to split "should create a new task" into "should create a new task" and it "should assign the current user''s id to the new task". Also there should be no need to put an expectation on Task (or @task?) in "should redirect to /tasks". That''s my interpretation of things, please correct me if I misunderstood. Ashley
aslak hellesoy
2007-Jul-12 09:33 UTC
[rspec-users] Agh, this is annoying. Why is this happening?
This doesn''t make sense: Task.should_receive(:user_id).with(@user.id).and_return(true) It means that you expect this: foo = Task.user_id(@user_id) # foo will be true I suspect you rather want this to happen: @task.user_id = @user_id In which case your mock setup should be: @task.should_receive(:user_id=).with(@user.id) Aslak On 7/12/07, Fischer, Daniel <daniel at danielfischer.com> wrote:> My problem: > > Mock ''Task_1005'' received unexpected message :user_id= with (1) > > No matter what I do to try to stub that out it will still fail out and give > me that message. > > Here is my spec > > > describe TasksController, "handling POST /tasks" do > before(:each) do > @task = mock_model(Task, :to_param => "1", :save => true) > Task.stub!(:new).and_return(@task) > @user = mock_model(User) > @user.stub!(:id).and_return(1) > @user.stub!(:login).and_return("moo") > User.stub!(:find).and_return(@user) > @params = {} > end > > def do_post > @request.session[:user] = @ user.id > post :create, :task => @params > end > > it "should create a new task" do > Task.should_receive(:user_id).with(@user.id).and_return(true) > > Task.should_receive(:new).with(@params).and_return(@task) > do_post > end > > it "should redirect to /tasks" do > Task.should_receive(:user_id).with(@user.id ).and_return(true) > do_post > response.should redirect_to(home_url) > end > end > > And my controller: > > def create > @task = Task.new(params[:task]) > @task.user_id = current_user.id > @task.status = Status.find_by_name(''in progress'') > > respond_to do |format| > if @task.save > flash[:notice] = ''Task was successfully created.'' > format.html { redirect_to home_url } > format.xml { head :created, :location => task_url(@task) } > else > flash[:error] = @task.errors > format.html { redirect_to home_url } > format.xml { render :xml => @ task.errors.to_xml } > end > end > end > > So obviously it''s failing with the current_user.id thing. I''m using > restful_authentication. > > Thanks! > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >
David Chelimsky
2007-Jul-12 11:42 UTC
[rspec-users] Agh, this is annoying. Why is this happening?
On 7/12/07, aslak hellesoy <aslak.hellesoy at gmail.com> wrote:> This doesn''t make sense: > > Task.should_receive(:user_id).with(@user.id).and_return(true) > > It means that you expect this: > > foo = Task.user_id(@user_id) # foo will be true > > I suspect you rather want this to happen: > > @task.user_id = @user_id > > In which case your mock setup should be: > > @task.should_receive(:user_id=).with(@user.id)This is also supported by the error message you are getting: Mock ''Task_1005'' received unexpected message :user_id= with (1) It states that the unexpected message is :user_id=, not :user_id. Easy enough thing to miss. Very subtle. But I''ll bet you won''t miss it again :) Cheers, David> > Aslak > > On 7/12/07, Fischer, Daniel <daniel at danielfischer.com> wrote: > > My problem: > > > > Mock ''Task_1005'' received unexpected message :user_id= with (1) > > > > No matter what I do to try to stub that out it will still fail out and give > > me that message. > > > > Here is my spec > > > > > > describe TasksController, "handling POST /tasks" do > > before(:each) do > > @task = mock_model(Task, :to_param => "1", :save => true) > > Task.stub!(:new).and_return(@task) > > @user = mock_model(User) > > @user.stub!(:id).and_return(1) > > @user.stub!(:login).and_return("moo") > > User.stub!(:find).and_return(@user) > > @params = {} > > end > > > > def do_post > > @request.session[:user] = @ user.id > > post :create, :task => @params > > end > > > > it "should create a new task" do > > Task.should_receive(:user_id).with(@user.id).and_return(true) > > > > Task.should_receive(:new).with(@params).and_return(@task) > > do_post > > end > > > > it "should redirect to /tasks" do > > Task.should_receive(:user_id).with(@user.id ).and_return(true) > > do_post > > response.should redirect_to(home_url) > > end > > end > > > > And my controller: > > > > def create > > @task = Task.new(params[:task]) > > @task.user_id = current_user.id > > @task.status = Status.find_by_name(''in progress'') > > > > respond_to do |format| > > if @task.save > > flash[:notice] = ''Task was successfully created.'' > > format.html { redirect_to home_url } > > format.xml { head :created, :location => task_url(@task) } > > else > > flash[:error] = @task.errors > > format.html { redirect_to home_url } > > format.xml { render :xml => @ task.errors.to_xml } > > end > > end > > end > > > > So obviously it''s failing with the current_user.id thing. I''m using > > restful_authentication. > > > > Thanks! > > > > _______________________________________________ > > rspec-users mailing list > > rspec-users at rubyforge.org > > http://rubyforge.org/mailman/listinfo/rspec-users > > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >