简体   繁体   English

使用Shoulda测试delete:destroy时,@ controller为零

[英]@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/patch/testunit/test/unit/ui/teamcity/testrunner.rb:191:in `start'

You non-plural name for test but the controller is ParticipationsController: 您使用非复数名称进行测试,但控制器为ParticipationsController:

/test/functional/participation_controller_test.rb /test/functional/participation_controller_test.rb

Change the Class name and filename to: 将类名称和文件名更改为:

ParticipationsControllerTest ParticipationsControllerTest

participations_controller_test.rb 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. 您必须将should_redirect_to包装到一个函数中,现在当该类由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. 在我的情况下,该控制器不存在,然后Rails没有加载任何控制器,并向我抛出了您遇到的错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM