Rspec,确保在Rails控制器类上调用方法

时间:2013-05-17 09:12:45

标签: ruby-on-rails testing rspec

我的应用程序控制器中包含一些第三方库。

require 'new_relic/agent/method_tracer'
class ApplicationController < ApplicationController::Base
end

我想在我的其他控制器中使用它,例如在我的SearchController

class SearchController < ApplicationController
    add_method_tracer :show, 'Custom/FU'
end

我知道,我可以通过rspec检查控制器中库提供的方法是否存在:

require 'spec_helper'

describe SearchController do

  it "has newrelic method tracer enabled" do
    SearchController.should respond_to :add_method_tracer
  end
end

但是这个解决方案没有检查正确的方法参数。

我如何确保(通过rspec测试)SearchController具有:

add_method_tracer :show, 'Custom/FU'

行存在,并使用适当的参数调用它?

简单地说,我想要一个测试,当有人意外地从控制器中删除add_method_tracer时,它会失败...并且......我想要确保,使用适当的参数调用该方法。

1 个答案:

答案 0 :(得分:0)

我不知道这个宝石,但我看了at the code并发现it checks如果它已经使用trace_method_exists?做了它应该做的事情。

所以你可以尝试这样的事情:

it "is tracing #show on metric Custom/FU" do
  expect(SearchController.trace_method_exists?(:show, "Custom/FU")).to be_false
end