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

aslak hellesoy aslak.hellesoy at gmail.com
Thu Jul 12 05:33:08 EDT 2007


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
>


More information about the rspec-users mailing list