minitest-vcr icon indicating copy to clipboard operation
minitest-vcr copied to clipboard

There can be only one gem that calls #before or #after...

Open Aryk opened this issue 7 years ago • 6 comments

If any of other test helping library calls ::MiniTest::Spec.before or ::MiniTest::Spec.after, the last one called wins. That should not be the case. It makes for really hard to track down bugs,

  ::MiniTest::Spec.before :each, &run_before
  ::MiniTest::Spec.after :each, &run_after

Instead we should include a module with super for setup and teardown. All those methods do is redefine setup and teardown but they weren't designed to be used on the top level class.

It was supposed to be used in nested describe where each nesting is its own subclass.

Aryk avatar Jul 02 '17 07:07 Aryk

The solution is pretty simple. It is just this:

  module Spec
    module SetupAndTeardown
      def setup
        super
        if metadata[:vcr]
          options = metadata[:vcr].is_a?(Hash) ? metadata[:vcr] : {}
          VCR.insert_cassette StringHelpers.vcr_path(self), options
        end
      end

      def teardown
        super
        ::VCR.eject_cassette if metadata[:vcr]
      end
    end

    def self.configure!
      ::MiniTest::Spec.send(:include, SetupAndTeardown)
    end

  end # Spec

Can you add this in?

Aryk avatar Jul 02 '17 07:07 Aryk

Sure. You want to open a PR?

mfpiccolo avatar Jul 03 '17 18:07 mfpiccolo

I'm trying to fork and run specs. On 2.4.1 with a clean bundle install

rake test gives me

  1) Error:
MinitestVcr::Spec::an it with metadata::with a nested example group#test_0002_can supply metadata:
NoMethodError: undefined method `<<' for {:read_timeout=>60, :continue_timeout=>nil, :debug_output=>nil}:Hash
Did you mean?  <
    /Users/aryk/.rvm/rubies/ruby-2.4.1/lib/ruby/2.4.0/net/protocol.rb:219:in `writing'
    /Users/aryk/.rvm/rubies/ruby-2.4.1/lib/ruby/2.4.0/net/protocol.rb:202:in `write'
    /Users/aryk/.rvm/rubies/ruby-2.4.1/lib/ruby/2.4.0/net/http/generic_request.rb:334:in `write_header'
    /Users/aryk/.rvm/rubies/ruby-2.4.1/lib/ruby/2.4.0/net/http/generic_request.rb:127:in `exec'
    /Users/aryk/.rvm/rubies/ruby-2.4.1/lib/ruby/2.4.0/net/http.rb:1444:in `block in transport_request'
    /Users/aryk/.rvm/rubies/ruby-2.4.1/lib/ruby/2.4.0/net/http.rb:1443:in `catch'
    /Users/aryk/.rvm/rubies/ruby-2.4.1/lib/ruby/2.4.0/net/http.rb:1443:in `transport_request'
    /Users/aryk/.rvm/rubies/ruby-2.4.1/lib/ruby/2.4.0/net/http.rb:1416:in `request'
    /Users/aryk/.rvm/gems/ruby-2.4.1@minitest-vcr/gems/webmock-1.24.6/lib/webmock/http_lib_adapters/net_http.rb:97:in `block in request'
    /Users/aryk/.rvm/gems/ruby-2.4.1@minitest-vcr/gems/webmock-1.24.6/lib/webmock/http_lib_adapters/net_http.rb:110:in `block in request'
    /Users/aryk/.rvm/rubies/ruby-2.4.1/lib/ruby/2.4.0/net/http.rb:877:in `start'
    /Users/aryk/.rvm/gems/ruby-2.4.1@minitest-vcr/gems/webmock-1.24.6/lib/webmock/http_lib_adapters/net_http.rb:109:in `request'
    /Users/aryk/.rvm/rubies/ruby-2.4.1/lib/ruby/2.4.0/net/http.rb:1165:in `get'
    /Users/aryk/.rvm/gems/ruby-2.4.1@minitest-vcr/gems/faraday-0.12.1/lib/faraday/adapter/net_http.rb:78:in `perform_request'
    /Users/aryk/.rvm/gems/ruby-2.4.1@minitest-vcr/gems/faraday-0.12.1/lib/faraday/adapter/net_http.rb:38:in `block in call'
    /Users/aryk/.rvm/gems/ruby-2.4.1@minitest-vcr/gems/faraday-0.12.1/lib/faraday/adapter/net_http.rb:85:in `with_net_http_connection'
    /Users/aryk/.rvm/gems/ruby-2.4.1@minitest-vcr/gems/faraday-0.12.1/lib/faraday/adapter/net_http.rb:33:in `call'
    /Users/aryk/.rvm/gems/ruby-2.4.1@minitest-vcr/gems/faraday-0.12.1/lib/faraday/request/url_encoded.rb:15:in `call'
    /Users/aryk/.rvm/gems/ruby-2.4.1@minitest-vcr/gems/faraday-0.12.1/lib/faraday/rack_builder.rb:139:in `build_response'
    /Users/aryk/.rvm/gems/ruby-2.4.1@minitest-vcr/gems/faraday-0.12.1/lib/faraday/connection.rb:386:in `run_request'
    /Users/aryk/.rvm/gems/ruby-2.4.1@minitest-vcr/gems/faraday-0.12.1/lib/faraday/connection.rb:149:in `get'
    /Users/aryk/Development/contributions/minitest-vcr/test/minitest-vcr/spec_test.rb:28:in `block (4 levels) in <top (required)>'

Aryk avatar Jul 03 '17 19:07 Aryk

Upgrading webmock fixes it, so going to update it.

Aryk avatar Jul 03 '17 19:07 Aryk

PR submitted.

Aryk avatar Jul 03 '17 19:07 Aryk

Is there any reason why this is coupled to Minitest::Spec and not Minitest::Test?

The setup and teardown exist on that class, and I'm not sure why this has to be coupled to Minitest::Spec.

Thoughts?

Aryk avatar Jul 04 '17 17:07 Aryk