File: expect_message_using_expect.feature

package info (click to toggle)
ruby-rspec-mocks 2.14.5-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 868 kB
  • ctags: 725
  • sloc: ruby: 8,227; makefile: 4
file content (107 lines) | stat: -rw-r--r-- 3,254 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
Feature: expect message using `expect`

  Use `expect(object).to receive(:message)` to set an expectation that
  `object` should receive the message `:message` before the example is
  completed.

  Note: You can use `expect_any_instance_of` when you don't have a reference
  to the object that receives a message in your test. For more information,
  see the message_expectations/expect_any_instance_of feature.

  Scenario: expect a message
    Given a file named "spec/account_spec.rb" with:
      """ruby
      require "account"

      describe Account do
        context "when closed" do
          it "logs an account closed message" do
            logger = double("logger")
            account = Account.new logger

            expect(logger).to receive(:account_closed)

            account.close
          end
        end
      end
      """
    And a file named "lib/account.rb" with:
      """ruby
      Account = Struct.new(:logger) do
        def close
          logger.account_closed
        end
      end
      """
    When I run `rspec spec/account_spec.rb`
    Then the output should contain "1 example, 0 failures"

  Scenario: expect a message with an argument
    Given a file named "spec/account_spec.rb" with:
      """ruby
      require "account"

      describe Account do
        context "when closed" do
          it "logs an account closed message" do
            logger = double("logger")
            account = Account.new logger

            expect(logger).to receive(:account_closed).with(account)

            account.close
          end
        end
      end
      """
    And a file named "lib/account.rb" with:
      """ruby
      Account = Struct.new(:logger) do
        def close
          logger.account_closed(self)
        end
      end
      """
    When I run `rspec spec/account_spec.rb`
    Then the output should contain "1 example, 0 failures"

  Scenario: provide a return value
    Given a file named "spec/message_expectation_spec.rb" with:
      """ruby

      describe "a message expectation with a return value" do
        context "specified in a block" do
          it "returns the specified value" do
            object = double("object")
            expect(object).to receive(:message) { :return_value }
            object.message.should eq(:return_value)
          end
        end

        context "specified with and_return" do
          it "returns the specified value" do
            object = double("object")
            expect(object).to receive(:message).and_return(:return_value)
            object.message.should eq(:return_value)
          end
        end
      end
      """
    When I run `rspec spec/message_expectation_spec.rb`
    Then the output should contain "2 examples, 0 failures"

  Scenario: expect a specific number of calls
    Given a file named "spec/message_count_spec.rb" with:
      """ruby
      describe "a message expectation with a count" do
        it "passes if the expected number of calls happen" do
          string = "hi"
          expect(string).to receive(:length).exactly(3).times

          3.times { string.length }
        end
      end
      """
    When I run `rspec spec/message_count_spec.rb`
    Then the output should contain "1 example, 0 failures"