Shopify / statsd-instrument

A StatsD client for Ruby apps. Provides metaprogramming methods to inject StatsD instrumentation into your code.
http://shopify.github.io/statsd-instrument
MIT License
570 stars 94 forks source link

Rubocop Cop for nested assertions #307

Closed sambostock closed 1 year ago

sambostock commented 2 years ago

It should be possible to write a Rubocop Cop for the following

Bad

test "something" do
  assert_statsd_increment("metric.1") do
    assert_statsd_increment("metric.2") do
  # ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Capture datagrams instead of nesting assertion blocks
      # ...
        assert_statsd_increment("metric.n") do
      # ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Capture datagrams instead of nesting assertion blocks
          # actual work
        end
      # ...
    end
  end
end

Good

test "something" do
  capture_statsd_datagrams do
    # actual work
  end

  assert_statsd_increment("metric.1", datagrams: datagrams)
  assert_statsd_increment("metric.2", datagrams: datagrams)
  # ...
  assert_statsd_increment("metric.n", datagrams: datagrams)
end

Should we create such a Cop?

rafaelzimmermann commented 1 year ago

I am sorry if I missed something, but I believe this is already possible. For instance I have a test written like this:


class SomethingTest
  include StatsD::Instrument::Assertions
  include StatsD::Instrument::Helpers

  test "my test" do
    datagrams = capture_statsd_datagrams do
      invoke_something_to_be_tested
    end

    assert_statsd_increment("metric.name", datagrams:, times: 1, sample_rate: 1.0, tags: { "status": "ok"})
  end
end