GithubHelp home page GithubHelp logo

Comments (3)

workato-integration avatar workato-integration commented on June 27, 2024

https://new-relic.atlassian.net/browse/NR-277524

from newrelic-ruby-agent.

kaylareopelle avatar kaylareopelle commented on June 27, 2024

The example above was created with this class:

# lib/bowling.rb

require 'newrelic_rpm'

class Bowling
  attr_reader :score

  def initialize
    @score = 0
  end

  def hit(pin_count)
    @score += pin_count
  end

  add_method_tracer :hit
end

And this spec:

# spec/bowling_spec.rb

require 'bowling'

RSpec.describe Bowling, "#score" do
  context "with no strikes or spares" do
   # passes 
    it "sums the pin count for each roll" do
      bowling = Bowling.new
      20.times { bowling.hit(4) }
      expect(bowling.score).to eq 80
    end
   
    # fails with error
    it "does not like to be prepended" do
      allow_any_instance_of(Bowling)
        .to receive(:hit)
        .and_return(300)
    end
  end
end

With this Gemfile:

source 'https://rubygems.org'

gem "newrelic_rpm", "~> 9.10"
gem "rspec", "~> 3.13"

The example was drawn from: https://rspec.info/
It was executed using: $ NEW_RELIC_AGENT_ENABLED=false bin/rspec

from newrelic-ruby-agent.

kaylareopelle avatar kaylareopelle commented on June 27, 2024

The agent needs to be able to add method tracers in cases where the API is invoked before the agent is loaded. This means the behavior of add_method_tracer cannot be contingent upon whether the agent is enabled.

We have a feature request open that may get around this. #2701 would allow method tracers to be added by configuration rather than declaring them directly in your code.

For anyone who encounters this problem, we have a few workarounds to recommend:

  1. You could refactor your tests to stub a single instance of a class rather than any instance of a class. The following code didn't raise the RSpec prepend error:

Option A:

    let(:bowling) do
      b = bowling.new

      allow(b).to receive(:hit).and_return(100)   
      
      b
    end

Option B:

      let(:bowling) { Bowling.new }

      before do
        allow(bowling)
          .to receive(:hit)
          .and_return(100)
      end
  1. If you're using factory_bot, you could update the factory definition for the class to stub the return value.

  2. Inside the spec/spec_helper.rb file, you could overwrite the add_method_tracer method to remove its logic before RSpec is configured. This could be done using a one-liner, like:

  class Module; def add_method_tracer(*_args); end; end

from newrelic-ruby-agent.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.