Sha256: e4229d0e1b445e41098a558f8b3f8825c86b3cc04e61802a9e1ae3fd4c49b776

Contents?: true

Size: 1.79 KB

Versions: 154

Compression:

Stored size: 1.79 KB

Contents

Feature: stub a chain of methods

  Use the `stub_chain` method to stub a chain of two or more methods in one
  statement.  Method chains are considered a design smell, but it's not really
  the method chain itself that is the problem - it's the dependency chain
  represented by a chain of messages to different objects:

      foo.get_bar.get_baz

  This is a Law of Demeter violation if `get_bar` returns an object other than
  `foo`, and `get_baz` returns yet another object.

  Fluent interfaces look similar from a caller's perspective, but don't
  represent a dependency chain (the caller depends only on the object it is
  calling). Consider this common example from Ruby on Rails:

      Article.recent.by(current_user)

  The `recent` and `by` methods return the same object, so this is not a Law of
  Demeter violation.

  Scenario: stub a chain of methods
    Given a file named "stub_chain_spec.rb" with:
      """
      describe "stubbing a chain of methods" do
        subject { Object.new }

        context "given symbols representing methods" do
          it "returns the correct value" do
            subject.stub_chain(:one, :two, :three).and_return(:four)
            subject.one.two.three.should eq(:four)
          end
        end

        context "given a hash at the end" do
          it "returns the correct value" do
            subject.stub_chain(:one, :two, :three => :four)
            subject.one.two.three.should eq(:four)
          end
        end

        context "given a string of methods separated by dots" do
          it "returns the correct value" do
            subject.stub_chain("one.two.three").and_return(:four)
            subject.one.two.three.should eq(:four)
          end
        end
      end
      """
    When I run `rspec stub_chain_spec.rb`
    Then the examples should all pass

Version data entries

154 entries across 100 versions & 14 rubygems

Version Path
classiccms-0.7.5 vendor/bundle/gems/rspec-mocks-2.10.1/features/method_stubs/stub_chain.feature
classiccms-0.7.5 vendor/bundle/gems/rspec-mocks-2.9.0/features/method_stubs/stub_chain.feature
classiccms-0.7.4 vendor/bundle/gems/rspec-mocks-2.10.1/features/method_stubs/stub_chain.feature
classiccms-0.7.4 vendor/bundle/gems/rspec-mocks-2.9.0/features/method_stubs/stub_chain.feature
classiccms-0.7.3 vendor/bundle/gems/rspec-mocks-2.10.1/features/method_stubs/stub_chain.feature
classiccms-0.7.3 vendor/bundle/gems/rspec-mocks-2.9.0/features/method_stubs/stub_chain.feature
tnargav-1.3.3 vendor/bundle/ruby/1.9.1/gems/rspec-mocks-2.11.3/features/method_stubs/stub_chain.feature
tnargav-1.2.3 vendor/bundle/ruby/1.9.1/gems/rspec-mocks-2.11.3/features/method_stubs/stub_chain.feature
classiccms-0.7.2 vendor/bundle/gems/rspec-mocks-2.10.1/features/method_stubs/stub_chain.feature
classiccms-0.7.2 vendor/bundle/gems/rspec-mocks-2.9.0/features/method_stubs/stub_chain.feature
classiccms-0.7.1 vendor/bundle/gems/rspec-mocks-2.10.1/features/method_stubs/stub_chain.feature
classiccms-0.7.1 vendor/bundle/gems/rspec-mocks-2.9.0/features/method_stubs/stub_chain.feature
classiccms-0.7.0 vendor/bundle/gems/rspec-mocks-2.10.1/features/method_stubs/stub_chain.feature
classiccms-0.7.0 vendor/bundle/gems/rspec-mocks-2.9.0/features/method_stubs/stub_chain.feature
sunrise-cms-0.5.0.rc1 vendor/bundle/ruby/1.9.1/gems/rspec-mocks-2.10.1/features/method_stubs/stub_chain.feature
classiccms-0.6.9 vendor/bundle/gems/rspec-mocks-2.10.1/features/method_stubs/stub_chain.feature
classiccms-0.6.9 vendor/bundle/gems/rspec-mocks-2.9.0/features/method_stubs/stub_chain.feature
classiccms-0.6.8 vendor/bundle/gems/rspec-mocks-2.10.1/features/method_stubs/stub_chain.feature
classiccms-0.6.8 vendor/bundle/gems/rspec-mocks-2.9.0/features/method_stubs/stub_chain.feature
classiccms-0.6.7 vendor/bundle/gems/rspec-mocks-2.9.0/features/method_stubs/stub_chain.feature