gpt4 book ai didi

ruby-on-rails - Rspec,确保在 Rails Controller 类上调用方法

转载 作者:行者123 更新时间:2023-11-28 20:01:20 24 4
gpt4 key购买 nike

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

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

我想在我的其他 Controller 中使用它,例如在我的 SearchController 中

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

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

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'

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

简单地说,我想要一个测试,当有人意外地从 Controller 中删除 add_method_tracer 时测试失败......并且......我想要确保,该方法是用正确的参数调用的。

最佳答案

我不知道这个 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

关于ruby-on-rails - Rspec,确保在 Rails Controller 类上调用方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16605426/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com