[rspec-users] Agh, this is annoying. Why is this happening?

David Chelimsky dchelimsky at gmail.com
Thu Jul 12 07:42:16 EDT 2007


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
>


More information about the rspec-users mailing list