Module: Umlaut::TestHelp::TestWithCassette

Defined in:
lib/umlaut/test_help.rb

Overview

Keep it in a seperate module so people can include just that if they want Umlaut::TestHelp::TestWithCassette. If you've already included Umlaut::TestHelp into your

Instance Method Summary collapse

Instance Method Details

#test_with_cassette(name, group = nil, vcr_options = {}, &block) ⇒ Object

Helper to create a Test::Unit style test that is wrapped in VCR.use_cassette for testing. If you supply a 'group' option, then the cassettes will be placed on the file system in a directory based on that group, and the VCR cassettes will also be tagged with that group name.

Extract this whole thing to a gem for sharing?

An alternative to this method is using rspec (but not in Umlaut, we don't use rspec) OR using minitest-rails or minitest-spec-rails with minitest/spec style and the minitest-vcr gem. I've had mixed success with minitest/spec in rails.

extend TestWithCassette
test_with_cassette("do something", :group) do
  assert_...
end

117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/umlaut/test_help.rb', line 117

def test_with_cassette(name, group = nil, vcr_options ={}, &block)
  # cribbed from Rails and modified for VCR
  # https://github.com/rails/rails/blob/b451de0d6de4df6bc66b274cec73b919f823d5ae/activesupport/lib/active_support/testing/declarative.rb#L25

  test_name_safe = name.gsub(/\s+/,'_')

  test_method_name = "test_#{test_name_safe}".to_sym

  raise "#{test_method_name} is already defined in #{self}" if methods.include?(test_method_name)

  cassette_name = vcr_options.delete(:cassette)
  unless cassette_name
    # calculate default cassette name from test name
    cassette_name = test_name_safe
    # put in group subdir if group
    cassette_name = "#{group}/#{cassette_name}" if group
  end

  # default tag with groupname, can be over-ridden.
  vcr_options = {:tag => group}.merge(vcr_options) if group

  if block_given?
    define_method(test_method_name) do
      VCR.use_cassette(cassette_name , vcr_options) do
        instance_eval &block
      end
    end
  else
    define_method(test_method_name) do
      flunk "No implementation provided for #{name}"
    end
  end
end