如何告诉Rails卸载控制器(在Rspec测试中)

时间:2014-07-10 08:15:39

标签: ruby-on-rails rspec

在控制器测试中,我想检查是否按预期定义了新的遗物方面。为了测试它,我做了类似的事情:

describe 'newrelic_ignore' do
  let(:load_class) do
    load Rails.root.join("app/controllers/foo_controller.rb")
  end

  before do
    Project::Application.send(:remove_const, 'FooController')
    stub_const('NewRelic::Agent::Instrumentation::ControllerInstrumentation', Module.new)
  end

  it "should inject newrelic_ignore" do
    Module.any_instance.should_receive(:newrelic_ignore).with(no_args())
    load_class
  end
end

似乎我试图在错误的位置移除常量导致:

pry(main)> Project::Application.send(:remove_const, 'FooController')
NameError: constant Project::Application::FooController not defined

有人可以给我一些提示吗?此外,如果有人知道更方便的测试方法,我将很高兴听到它。

1 个答案:

答案 0 :(得分:1)

这应该有效

Object.send(:remove_const, :FooController)