Sha256: b461794ca1a5ed177c4db73513c3b339c0ee5f2ccc6f7f83770afb6adf7acbe1

Contents?: true

Size: 1.98 KB

Versions: 7

Compression:

Stored size: 1.98 KB

Contents

Feature: Wrapping the original implementation

  Use `and_wrap_original` to modify a partial double's original response. This can be useful
  when you want to utilise an external object but mutate it's response. For example if an
  API returns a large amount of data and for test purposes you'd like to trim it down. You can
  also use it to configure the default response for most arguments, and then override that for
  specific arguments using `with`.

  Note: `and_wrap_original` is only supported on partial doubles, as normal test doubles do
  not have an original implementation.

  Background:
    Given a file named "lib/api.rb" with:
      """ruby
      class API
        def self.solve_for(x)
          (1..x).to_a
        end
      end
      """

  Scenario: `and_wrap_original` wraps the original partial double response
    Given a file named "spec/and_wrap_original_spec.rb" with:
      """ruby
      require 'api'

      RSpec.describe "and_wrap_original" do
        it "responds as it normally would, modified by the block" do
          expect(API).to receive(:solve_for).and_wrap_original { |m, *args| m.call(*args).first(5) }
          expect(API.solve_for(100)).to eq [1,2,3,4,5]
        end
      end
      """
    When I run `rspec spec/and_wrap_original_spec.rb`
    Then the examples should all pass

  Scenario: `and_wrap_original` can configure a default response that can be overriden for specific args
    Given a file named "spec/and_wrap_original_spec.rb" with:
      """ruby
      require 'api'

      RSpec.describe "and_wrap_original" do
        it "can be overriden for specific arguments using #with" do
          allow(API).to receive(:solve_for).and_wrap_original { |m, *args| m.call(*args).first(5) }
          allow(API).to receive(:solve_for).with(2).and_return([3])

          expect(API.solve_for(20)).to eq [1,2,3,4,5]
          expect(API.solve_for(2)).to eq [3]
        end
      end
      """
    When I run `rspec spec/and_wrap_original_spec.rb`
    Then the examples should all pass

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
opal-rspec-0.8.0 rspec-mocks/upstream/features/configuring_responses/wrapping_the_original_implementation.feature
opal-rspec-0.8.0.alpha3 rspec-mocks/upstream/features/configuring_responses/wrapping_the_original_implementation.feature
opal-rspec-0.8.0.alpha2 rspec-mocks/upstream/features/configuring_responses/wrapping_the_original_implementation.feature
opal-rspec-0.8.0.alpha1 rspec-mocks/upstream/features/configuring_responses/wrapping_the_original_implementation.feature
opal-rspec-0.7.1 rspec-mocks/upstream/features/configuring_responses/wrapping_the_original_implementation.feature
opal-rspec-0.7.0 rspec-mocks/upstream/features/configuring_responses/wrapping_the_original_implementation.feature
opal-rspec-0.7.0.rc.2 rspec-mocks/upstream/features/configuring_responses/wrapping_the_original_implementation.feature