简体   繁体   中英

Rspec, ensuring a method is called on a Rails controller class

I have some 3rd party library included in my application controller.

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

I want use it in my other controllers, for example in my SearchController

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

I know, I can check via rspec for the existence of the method provided by the library in the controller:

require 'spec_helper'

describe SearchController do

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

But this solution doesn't check the proper method arguments.

How I can ensure (test via rspec) the SearchController has the:

add_method_tracer :show, 'Custom/FU'

line present, and it is called with proper arguments ?

Simply said, I want a test, which fails when somebody removes by accident the add_method_tracer from the controller... AND... I want ensure, the method is called with proper arguments.

I don't know this gem, but I took a look at the code and found that it checks if it already did what it is supposed to do using trace_method_exists? .

So you could try something like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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