开发者

@controller is nil when testing delete :destroy with Shoulda

The code below:

  context "should destroy participation" do
    setup do
      @p = Factory.create :participation
      delete :destroy, :id => @p.id.to_param
    end

    should_redirect_to(:controller => 'configuration', :action => 'edit')   
  end

Gives me the error below, any idea why?

RuntimeError: @controller is nil: make sure you set it in your test's setup method.
    /test/functional/participation_controller_test.rb:30:in `__bind_1279893888_614853'
    /Applications/RubyMine 2.0.2.app/rb/testing/patch/testunit/test/unit/ui/testrunnermediator.rb:36:in `run_suite'
    /Applications/RubyMine 2.0.2.app/rb/testing/patch/testunit/test/unit/ui/teamcity/testrunner.rb:215:in `start_mediator'
    /Applications/RubyMine 2.0.2.app/rb/testing/patc开发者_如何学Pythonh/testunit/test/unit/ui/teamcity/testrunner.rb:191:in `start'


You non-plural name for test but the controller is ParticipationsController:

/test/functional/participation_controller_test.rb

Change the Class name and filename to:

ParticipationsControllerTest

participations_controller_test.rb


You have to wrap should_redirect_to into a function, right now it is executed when the class is loaded by ruby.

context "should destroy participation" do
  setup do
    @p = Factory.create :participation
  end

  should "redirect ...." do
    delete :destroy, :id => @p.id.to_param
    should_redirect_to(:controller => 'configuration', :action => 'edit')
  end 
end


Does the controller exist? In my case, the controller did not exist, then rails loaded no controller and threw me the error that you were getting.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜