简体   繁体   English

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

[英]How to tell Rails to unload a controller (in Rspec test)

In a controller test i want to check if new relic aspects are defined as expected. 在控制器测试中,我想检查是否按预期定义了新的文物特征。 To test it i'm doing something like: 为了测试它,我正在做类似的事情:

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

It seems that i'm trying to remove the constant at the wrong place resulting in: 似乎我正在尝试在错误的位置删除常量,导致:

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

Can anyone give me a hint there these reside? 有人可以给我提示这些住所吗? Additionally if someone knows a more convinient way to test this i would appreciate to hear about it. 此外,如果有人知道一种更方便的测试方法,我将不胜感激。

这应该工作

Object.send(:remove_const, :FooController)

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

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