【问题标题】:Rspec, ensuring a method is called on a Rails controller classRspec,确保在 Rails 控制器类上调用方法
【发布时间】:2013-05-17 09:12:45
【问题描述】:

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

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 时,它会失败......并且......我想确保使用正确的参数调用该方法。

【问题讨论】:

    标签: ruby-on-rails testing rspec


    【解决方案1】:

    我不知道这个 gem,但我看了一下 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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多