[rspec-users] How to do controller.stub(:some_method) at request test when using rspec2

Rob Aldred Rob.aldred at stardotstar.com
Thu Mar 3 09:29:15 EST 2011


On 3 Mar 2011, at 07:36, Jiang Guimin wrote:

> Hi,
>  I hava a controller
> 
>  class DynamicFlowsController < ApplicationController
> 
>   def create
>      @workflow = Workflow.find_by_id(params[:workflow_id])
>      table = @workflow.flex_table
>       save_u_and_node(table, @workflow.node_definition)
>       redirect_to "/#{table.name}"
>   end
> 
>  end
> 
>  and I had written some rspec codes
> 
>   describe "save attachment" do
> 
>    before(:each) do
>      workflow, table, form = init_stubs("baoxiaos", 1)
>      definition = workflow.node_definition
>      controller.should_receive(:save_u_and_node).with(table,
> definition)
>    end
> 
>    it "should be successful" do
>      file = UploadFile.new("a" * 10000, "testfile", "text/plain")
>      post '/workflows/1/baoxiaos', { :attachment => file}
>      response.should be_successful
>    end
> 
>  end
> 
> When I run the test, unfortunately rspec told me that "controller"  is
> nil, I got a fail test.
> So, I want to konw how to get the correct "controller" object.
> 
> Thanks
> Kayak
> 
> -- 
> Posted via http://www.ruby-forum.com/.
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users


Are you wanting to stub a method of the controller you are actually testing?
I'm pretty sure this is bad practice.





More information about the rspec-users mailing list