Sha256: d511f11c331997c8e456b3fff12b5302d2da0acedf2a1eca207803afe96d3552

Contents?: true

Size: 1.68 KB

Versions: 1

Compression:

Stored size: 1.68 KB

Contents

Feature: expect change

  Expect the execution of a block of code to change the state of an object.

  Background:
    Given a file named "lib/counter.rb" with:
      """ruby
      class Counter
        class << self
          def increment
            @count ||= 0
            @count += 1
          end

          def count
            @count ||= 0
          end
        end
      end
      """

  Scenario: expect change
    Given a file named "spec/example_spec.rb" with:
      """ruby
      require "counter"

      describe Counter, "#increment" do
        it "should increment the count" do
          expect { Counter.increment }.to change{Counter.count}.from(0).to(1)
        end

        # deliberate failure
        it "should increment the count by 2" do
          expect { Counter.increment }.to change{Counter.count}.by(2)
        end
      end
      """
    When I run `rspec spec/example_spec.rb`
    Then the output should contain "1 failure"
    Then the output should contain "expected result to have changed by 2, but was changed by 1"

  Scenario: expect no change
    Given a file named "spec/example_spec.rb" with:
      """ruby
      require "counter"

      describe Counter, "#increment" do
        it "should not increment the count by 1 (using not_to)" do
          expect { Counter.increment }.not_to change{Counter.count}
        end

        it "should not increment the count by 1 (using to_not)" do
          expect { Counter.increment }.to_not change{Counter.count}
        end
      end
      """
    When I run `rspec spec/example_spec.rb`
    Then the output should contain "2 failures"
    Then the output should contain "expected result not to have changed, but did change from 1 to 2"

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rspec-expectations-3.0.0.beta2 features/built_in_matchers/expect_change.feature