0

我有一个实现 VCR 的测试,如下所示:

  describe "DELETE destroy" do
    context 'for an Item that can be deleted' do
      it "destroys the requested item, assigns a flash message, and redirects to the items list" do
        VCR.use_cassette('example', :allow_playback_repeats => true) do
          new_item = FactoryGirl.create(:item, valid_attributes.merge!(category_ids: [FactoryGirl.create(:category).id]))
          expect {
            delete :destroy, {:id => new_item.to_param}
          }.to change(Item, :count).by(-1)
          flash[:notice].should eq(I18n.t(:success, obj: 'item', past_participle: 'deleted'))
          response.should redirect_to(vendor_items_path)
        end
      end
    end
...

当我运行测试时,VCR 会创建一个磁带,并且测试通过 rspec。但是,当我重新运行测试时,它失败并出现以下错误:

  1) Vendor::ItemsController DELETE destroy for an Item that can be deleted destroys the requested item, assigns a flash message, and redirects to the items list
     Failure/Error: expect {
       count should have been changed by -1, but was changed by 0
     # /Users/user/.rvm/gems/ruby-1.9.3-head@wdi/gems/rspec-expectations-2.13.0/lib/rspec/expectations/fail_with.rb:32:in `fail_with'
     # /Users/user/.rvm/gems/ruby-1.9.3-head@wdi/gems/rspec-expectations-2.13.0/lib/rspec/expectations/handler.rb:33:in `handle_matcher'
     # /Users/user/.rvm/gems/ruby-1.9.3-head@wdi/gems/rspec-expectations-2.13.0/lib/rspec/expectations/expectation_target.rb:34:in `to'
     # /Users/user/rails_projects/wdi/spec/controllers/vendor/items_controller_spec.rb:112:in `block (5 levels) in <top (required)>'
     # /Users/user/.rvm/gems/ruby-1.9.3-head@wdi/gems/vcr-2.5.0/lib/vcr/util/variable_args_block_caller.rb:9:in `call'
     # /Users/user/.rvm/gems/ruby-1.9.3-head@wdi/gems/vcr-2.5.0/lib/vcr/util/variable_args_block_caller.rb:9:in `call_block'
     # /Users/user/.rvm/gems/ruby-1.9.3-head@wdi/gems/vcr-2.5.0/lib/vcr.rb:173:in `use_cassette'
     # /Users/user/rails_projects/wdi/spec/controllers/vendor/items_controller_spec.rb:110:in `block (4 levels) in <top (required)>'
     # /Users/user/.rvm/gems/ruby-1.9.3-head@wdi/gems/rspec-core-2.13.1/lib/rspec/core/example.rb:114:in `instance_eval'
     # /Users/user/.rvm/gems/ruby-1.9.3-head@wdi/gems/rspec-core-2.13.1/lib/rspec/core/example.rb:114:in `block in run'
     # /Users/user/.rvm/gems/ruby-1.9.3-head@wdi/gems/rspec-rails-2.13.2/lib/rspec/rails/example/controller_example_group.rb:160:in `call'
     # /Users/user/.rvm/gems/ruby-1.9.3-head@wdi/gems/rspec-rails-2.13.2/lib/rspec/rails/example/controller_example_group.rb:160:in `block (2 levels) in <module:ControllerExampleGroup>'
     # /Users/user/.rvm/gems/ruby-1.9.3-head@wdi/gems/rspec-core-2.13.1/lib/rspec/core/extensions/instance_eval_with_args.rb:16:in `instance_exec'
     # /Users/user/.rvm/gems/ruby-1.9.3-head@wdi/gems/rspec-core-2.13.1/lib/rspec/core/extensions/instance_eval_with_args.rb:16:in `instance_eval_with_args'
     # /Users/user/.rvm/gems/ruby-1.9.3-head@wdi/gems/rspec-core-2.13.1/lib/rspec/core/example.rb:247:in `instance_eval_with_args'
     # /Users/user/.rvm/gems/ruby-1.9.3-head@wdi/gems/rspec-core-2.13.1/lib/rspec/core/hooks.rb:87:in `block (2 levels) in run'
     # /Users/user/.rvm/gems/ruby-1.9.3-head@wdi/gems/rspec-core-2.13.1/lib/rspec/core/hooks.rb:85:in `call'
     # /Users/user/.rvm/gems/ruby-1.9.3-head@wdi/gems/rspec-core-2.13.1/lib/rspec/core/hooks.rb:85:in `run'
     # /Users/user/.rvm/gems/ruby-1.9.3-head@wdi/gems/rspec-core-2.13.1/lib/rspec/core/hooks.rb:418:in `run_hook'
     # /Users/user/.rvm/gems/ruby-1.9.3-head@wdi/gems/rspec-core-2.13.1/lib/rspec/core/example_group.rb:329:in `run_around_each_hooks'
     # /Users/user/.rvm/gems/ruby-1.9.3-head@wdi/gems/rspec-core-2.13.1/lib/rspec/core/example.rb:256:in `with_around_each_hooks'
     # /Users/user/.rvm/gems/ruby-1.9.3-head@wdi/gems/rspec-core-2.13.1/lib/rspec/core/example.rb:111:in `run'
     # /Users/user/.rvm/gems/ruby-1.9.3-head@wdi/gems/rspec-core-2.13.1/lib/rspec/core/example_group.rb:390:in `block in run_examples'
     # /Users/user/.rvm/gems/ruby-1.9.3-head@wdi/gems/rspec-core-2.13.1/lib/rspec/core/example_group.rb:386:in `map'
     # /Users/user/.rvm/gems/ruby-1.9.3-head@wdi/gems/rspec-core-2.13.1/lib/rspec/core/example_group.rb:386:in `run_examples'
     # /Users/user/.rvm/gems/ruby-1.9.3-head@wdi/gems/rspec-core-2.13.1/lib/rspec/core/example_group.rb:371:in `run'
     # /Users/user/.rvm/gems/ruby-1.9.3-head@wdi/gems/rspec-core-2.13.1/lib/rspec/core/example_group.rb:372:in `block in run'
     # /Users/user/.rvm/gems/ruby-1.9.3-head@wdi/gems/rspec-core-2.13.1/lib/rspec/core/example_group.rb:372:in `map'
     # /Users/user/.rvm/gems/ruby-1.9.3-head@wdi/gems/rspec-core-2.13.1/lib/rspec/core/example_group.rb:372:in `run'
     # /Users/user/.rvm/gems/ruby-1.9.3-head@wdi/gems/rspec-core-2.13.1/lib/rspec/core/example_group.rb:372:in `block in run'
     # /Users/user/.rvm/gems/ruby-1.9.3-head@wdi/gems/rspec-core-2.13.1/lib/rspec/core/example_group.rb:372:in `map'
     # /Users/user/.rvm/gems/ruby-1.9.3-head@wdi/gems/rspec-core-2.13.1/lib/rspec/core/example_group.rb:372:in `run'
     # /Users/user/.rvm/gems/ruby-1.9.3-head@wdi/gems/rspec-core-2.13.1/lib/rspec/core/command_line.rb:28:in `block (2 levels) in run'
     # /Users/user/.rvm/gems/ruby-1.9.3-head@wdi/gems/rspec-core-2.13.1/lib/rspec/core/command_line.rb:28:in `map'
     # /Users/user/.rvm/gems/ruby-1.9.3-head@wdi/gems/rspec-core-2.13.1/lib/rspec/core/command_line.rb:28:in `block in run'
     # /Users/user/.rvm/gems/ruby-1.9.3-head@wdi/gems/rspec-core-2.13.1/lib/rspec/core/reporter.rb:34:in `report'
     # /Users/user/.rvm/gems/ruby-1.9.3-head@wdi/gems/rspec-core-2.13.1/lib/rspec/core/command_line.rb:25:in `run'
     # /Users/user/.rvm/gems/ruby-1.9.3-head@wdi/gems/rspec-core-2.13.1/lib/rspec/core/runner.rb:80:in `run'
     # /Users/user/.rvm/gems/ruby-1.9.3-head@wdi/gems/rspec-core-2.13.1/lib/rspec/core/runner.rb:17:in `block in autorun'

Finished in 5.48 seconds
12 examples, 1 failure

Failed examples:

rspec ./spec/controllers/vendor/items_controller_spec.rb:109 # Vendor::ItemsController DELETE destroy for an Item that can be deleted destroys the requested item, assigns a flash message, and redirects to the items list

初始测试的 VCR 日志信息(rspec 测试通过)

[Cassette: 'example'] Initialized with options: {:record=>:once, :match_requests_on=>[:method, :uri], :allow_unused_http_interactions=>true, :serialize_with=>:yaml, :persist_with=>:file_system, :allow_playback_repeats=>true}
[webmock] Handling request: [head https://s3.amazonaws.com/wdi_development/item_images/6/original/item.jpg] (disabled: false)
  [Cassette: 'example'] Initialized HTTPInteractionList with request matchers [:method, :uri] and 0 interaction(s): {  }
[webmock] Identified request type (recordable) for [head https://s3.amazonaws.com/wdi_development/item_images/6/original/item.jpg]
[Cassette: 'example'] Recorded HTTP interaction [head https://s3.amazonaws.com/wdi_development/item_images/6/original/item.jpg] => [404 ""]
[webmock] Handling request: [head https://s3.amazonaws.com/wdi_development/item_images/6/medium/item.jpg] (disabled: false)
[webmock] Identified request type (recordable) for [head https://s3.amazonaws.com/wdi_development/item_images/6/medium/item.jpg]
[Cassette: 'example'] Recorded HTTP interaction [head https://s3.amazonaws.com/wdi_development/item_images/6/medium/item.jpg] => [404 ""]
[webmock] Handling request: [head https://s3.amazonaws.com/wdi_development/item_images/6/thumb/item.jpg] (disabled: false)
[webmock] Identified request type (recordable) for [head https://s3.amazonaws.com/wdi_development/item_images/6/thumb/item.jpg]
[Cassette: 'example'] Recorded HTTP interaction [head https://s3.amazonaws.com/wdi_development/item_images/6/thumb/item.jpg] => [404 ""]
[webmock] Handling request: [head https://s3.amazonaws.com/wdi_development/item_images/6/thumb_item/item.jpg] (disabled: false)
[webmock] Identified request type (recordable) for [head https://s3.amazonaws.com/wdi_development/item_images/6/thumb_item/item.jpg]
[Cassette: 'example'] Recorded HTTP interaction [head https://s3.amazonaws.com/wdi_development/item_images/6/thumb_item/item.jpg] => [404 ""]

第二次测试的录像机日志信息(rspec 测试失败)

[Cassette: 'example'] Initialized with options: {:record=>:once, :match_requests_on=>[:method, :uri], :allow_unused_http_interactions=>true, :serialize_with=>:yaml, :persist_with=>:file_system, :allow_playback_repeats=>true}
[webmock] Handling request: [head https://s3.amazonaws.com/wdi_development/item_images/6/original/item.jpg] (disabled: false)
  [Cassette: 'example'] Initialized HTTPInteractionList with request matchers [:method, :uri] and 4 interaction(s): { [head https://s3.amazonaws.com/wdi_development/item_images/6/original/item.jpg] => [404 ""], [head https://s3.amazonaws.com/wdi_development/item_images/6/medium/item.jpg] => [404 ""], [head https://s3.amazonaws.com/wdi_development/item_images/6/thumb/item.jpg] => [404 ""], [head https://s3.amazonaws.com/wdi_development/item_images/6/thumb_item/item.jpg] => [404 ""] }
  [Cassette: 'example'] Checking if [head https://s3.amazonaws.com/wdi_development/item_images/6/original/item.jpg] matches [head https://s3.amazonaws.com/wdi_development/item_images/6/original/item.jpg] using [:method, :uri]
    [Cassette: 'example'] method (matched): current request [head https://s3.amazonaws.com/wdi_development/item_images/6/original/item.jpg] vs [head https://s3.amazonaws.com/wdi_development/item_images/6/original/item.jpg]
    [Cassette: 'example'] uri (matched): current request [head https://s3.amazonaws.com/wdi_development/item_images/6/original/item.jpg] vs [head https://s3.amazonaws.com/wdi_development/item_images/6/original/item.jpg]
  [Cassette: 'example'] Found matching interaction for [head https://s3.amazonaws.com/wdi_development/item_images/6/original/item.jpg] at index 0: [404 ""]
[webmock] Identified request type (stubbed_by_vcr) for [head https://s3.amazonaws.com/wdi_development/item_images/6/original/item.jpg]

为什么 allow_playback_repeats 不起作用?

4

0 回答 0