Coder Social home page Coder Social logo

Comments (6)

metaskills avatar metaskills commented on August 20, 2024

Hey @jrochkind this does look right. A few thoughts:

  1. Why do you have those library requires in your test_helper file? If those are in your test group of your Gemfile then the requires are moot? Just a note. Maybe you are just showing me which libs you use tho?

  2. I have a few integration Rails projects on my GitHub profile. Here is one for Rails 4.1 and latest MTSR. https://github.com/metaskills/mtsr_integration_v41

  3. I added a demo integration test and it worked great. metaskills/mtsr_integration_v41@980e8af

Given this, I suspect you have something wonky in your setup or the gems you are using. I would give minitest-vcr a look over.

from minitest-spec-rails.

metaskills avatar metaskills commented on August 20, 2024

I just looked over their source. FWIW, I use VCR a lot and just write a few of my own helpers. This gem does not add much. It also uses MiniTest meta data (https://github.com/mfpiccolo/minitest-vcr/blob/master/minitest-vcr.gemspec#L23) which I'm not a terrible fan of. Not sure how strong that interface is in MiniTest and even what happens to Rails apps which the Spec::DSL is inserted into.

from minitest-spec-rails.

jrochkind avatar jrochkind commented on August 20, 2024

Thanks. I am in the habit of require'ing gems used in tests in my test_helper, is there any reason it would cause a problem? I forget if things didn't work until I required them.

I will work on reducing to minimal reproducible case to see if any of the extra gems were causing the problem or what. Thanks for the demo working integration test to compare with.

Can you share the code for the 'few of your own helpers' you use with VCR, minitest/spec, and Rails? What I like from minitest-vcr is the ability to attach a few tokens to a describe context, and thereby have every example run in a use_cassette with the name/path of the cassette derived from the (possibly nested) description/examples. VCR does this for Rspec out of the box, as the VCR developer (who I think may also currently be the rspec developer?) believes this is the most convenient way to use VCR in spec syntax, and I agree with him. Curious if your helpers do this, or something else, either way I'm curious to see them.

from minitest-spec-rails.

metaskills avatar metaskills commented on August 20, 2024

Something like this:

module TestHelper
  module ServiceHelpers

    extend ActiveSupport::Concern

    included do
      VCR.configure do |c|
        c.cassette_library_dir = 'test/vcr_cassettes'
        c.hook_into :webmock
        c.ignore_localhost = true
      end
      extend VCRClassHelpers
    end

    module VCRClassHelpers

      def with_cassette(*args, &block)
        cassette_options = args.last.is_a?(Hash) ? args.pop : {}
        cassette_name    = args.first || name
        describe "with cassette #{cassette_name}" do
          use_vcr_cassette cassette_name, cassette_options
          instance_eval(&block)
        end
      end

      def use_cassette(name, options={})
        options = default_cassette_options.merge(options)
        VCR.use_cassette(name, options) { yield }
      end

      def use_vcr_cassette(cassette_name, cassette_options={})
        before do
          unless self.class.allow_http_connections?
            VCR.insert_cassette cassette_name, default_cassette_options.merge(cassette_options)
          end
        end
        after do
          unless self.class.allow_http_connections?
            VCR.eject_cassette
          end
        end
      end

      def allow_http_connections?
        ENV['ALLOW_HTTP_CONNECTIONS'] && ENV['ALLOW_HTTP_CONNECTIONS'] == 'true'
      end

      def default_cassette_options
        { serialize_with: :syck, allow_playback_repeats: true }
      end

    end


    private

    # VCR

    def use_cassette(name, options={})
      self.class.use_cassette(name, options) { yield }
    end

    def eject_cassette
      VCR.eject_cassette
    end

    def default_cassette_options
      self.class.default_cassette_options
    end

    # General

    def enable_net_connections
      WebMock.allow_net_connect!
    end

    def disable_net_connections
      WebMock.disable_net_connect! allow_localhost: true
    end


  end
end

from minitest-spec-rails.

metaskills avatar metaskills commented on August 20, 2024

The serialize_with: :syck option is really nice too. Allows your cassette data to be human readable if needed.

from minitest-spec-rails.

jrochkind avatar jrochkind commented on August 20, 2024

Can you include an example of how you use that helper method? The with_cassette method is what you use in your specs, and basically serves as a substitute for the describe, it creates a describe, such that all the examples in it will be wrapped in cassettes? With every example in the describe sharing the same cassette, or each having a different?

Honestly, figuring out how to do this stuff is non-trivial for those not as familiar with minitest/spec and VCR as you. Personally I do like the approach minitest-vcr takes of letting metadata on describe or examples trigger the VCR cassette -- this is the same approach vcr/rspec integration takes, although with minitest it does require an extra gem since minitest/spec doesn't support metadata out of the box.

But if you think you have a better way to do it, I'd encourage you to consider packaging it up in a gem with documentation, as I think many users are having more trouble with this than you might think.

from minitest-spec-rails.

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.