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
_______________________________________________
rspec-users mailing list
[email protected]
http://rubyforge.org/mailman/listinfo/rspec-users