Skip to content

Loading…

Add new feature for receive_counts on mocks #77

Merged
merged 2 commits into from

2 participants

@adomokos

Adding a new cucumber feature to cover:

  • .once
  • .twice
  • exactly(n).times
  • at_least(:once)
  • at_least(n).times
  • at_most(:once)
  • at_most(n).times
@adomokos

Any update on this pull request?

@dchelimsky dchelimsky commented on an outdated diff
features/message_expectations/receive_counts.feature
@@ -0,0 +1,212 @@
+Feature: receive counts
+
+ The implicit expectation is that the message passed to should_receive()
@dchelimsky RSpec member

The implicit expectation is one time, and can be overridden explicitly.

@adomokos
adomokos added a note

Should I remove this comment? I just copied it from the old rspec site.

@dchelimsky RSpec member

So is this whole thing just copied over from rspec-1?

@adomokos
adomokos added a note

Line 3 and 4 did. I'll remove it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@adomokos

Removed the incorrect description. Is there anything else I should change?

@dchelimsky dchelimsky merged commit 8d35db6 into rspec:master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Aug 26, 2011
  1. @adomokos
Commits on Sep 2, 2011
  1. @adomokos
Showing with 209 additions and 0 deletions.
  1. +209 −0 features/message_expectations/receive_counts.feature
View
209 features/message_expectations/receive_counts.feature
@@ -0,0 +1,209 @@
+Feature: receive counts
+
+ Scenario: expect a message once
+ Given a file named "spec/account_spec.rb" with:
+ """
+ class Account
+ attr_accessor :logger
+
+ def open
+ logger.account_opened
+ end
+ end
+
+ describe Account do
+ context "when opened" do
+ it "logger#account_opened was called once" do
+ logger = double("logger")
+ account = Account.new
+ account.logger = logger
+
+ logger.should_receive(:account_opened).once
+
+ account.open
+ end
+ end
+ end
+ """
+ When I run `rspec spec/account_spec.rb`
+ Then the output should contain "1 example, 0 failures"
+
+ Scenario: expect a message twice
+ Given a file named "spec/account_spec.rb" with:
+ """
+ class Account
+ attr_accessor :logger
+
+ def open
+ logger.account_opened
+ end
+ end
+
+ describe Account do
+ context "when opened" do
+ it "logger#account_opened was called once" do
+ logger = double("logger")
+ account = Account.new
+ account.logger = logger
+
+ logger.should_receive(:account_opened).twice
+
+ account.open
+ account.open
+ end
+ end
+ end
+ """
+ When I run `rspec spec/account_spec.rb`
+ Then the output should contain "1 example, 0 failures"
+
+ Scenario: expect a message 3 times
+ Given a file named "spec/account_spec.rb" with:
+ """
+ class Account
+ attr_accessor :logger
+
+ def open
+ logger.account_opened
+ end
+ end
+
+ describe Account do
+ context "when opened" do
+ it "logger#account_opened was called once" do
+ logger = double("logger")
+ account = Account.new
+ account.logger = logger
+
+ logger.should_receive(:account_opened).exactly(3).times
+
+ account.open
+ account.open
+ account.open
+ end
+ end
+ end
+ """
+ When I run `rspec spec/account_spec.rb`
+ Then the output should contain "1 example, 0 failures"
+
+ Scenario: expect a message at least (:once)
+ Given a file named "spec/account_spec.rb" with:
+ """
+ class Account
+ attr_accessor :logger
+
+ def open
+ logger.account_opened
+ end
+ end
+
+ describe Account do
+ context "when opened" do
+ it "logger#account_opened was called once" do
+ logger = double("logger")
+ account = Account.new
+ account.logger = logger
+
+ logger.should_receive(:account_opened).at_least(:once)
+
+ account.open
+ end
+ end
+ end
+ """
+ When I run `rspec spec/account_spec.rb`
+ Then the output should contain "1 example, 0 failures"
+
+
+ Scenario: expect a message at least (n) times
+ Given a file named "spec/account_spec.rb" with:
+ """
+ class Account
+ attr_accessor :logger
+
+ def open
+ logger.account_opened
+ end
+ end
+
+ describe Account do
+ context "when opened" do
+ it "logger#account_opened was called once" do
+ logger = double("logger")
+ account = Account.new
+ account.logger = logger
+
+ logger.should_receive(:account_opened).at_least(3).times
+
+ # Note that I am calling method under test 4 times
+ # and I specified it to be called at least 3 times
+ account.open
+ account.open
+ account.open
+ account.open
+ end
+ end
+ end
+ """
+ When I run `rspec spec/account_spec.rb`
+ Then the output should contain "1 example, 0 failures"
+
+ Scenario: expect a message at most (:once)
+ Given a file named "spec/account_spec.rb" with:
+ """
+ class Account
+ attr_accessor :logger
+
+ def open
+ logger.account_opened
+ end
+ end
+
+ describe Account do
+ context "when opened" do
+ it "logger#account_opened was called once" do
+ logger = double("logger")
+ account = Account.new
+ account.logger = logger
+
+ logger.should_receive(:account_opened).at_most(:once)
+
+ account.open
+ account.open
+ end
+ end
+ end
+ """
+ When I run `rspec spec/account_spec.rb`
+ Then the output should contain "expected: 1 time"
+ And the output should contain "received: 2 times"
+
+ Scenario: expect a message at most (n) times
+ Given a file named "spec/account_spec.rb" with:
+ """
+ class Account
+ attr_accessor :logger
+
+ def open
+ logger.account_opened
+ end
+ end
+
+ describe Account do
+ context "when opened" do
+ it "logger#account_opened was called once" do
+ logger = double("logger")
+ account = Account.new
+ account.logger = logger
+
+ logger.should_receive(:account_opened).at_most(2).times
+
+ account.open
+ account.open
+ end
+ end
+ end
+ """
+ When I run `rspec spec/account_spec.rb`
+ Then the output should contain "1 example, 0 failures"
Something went wrong with that request. Please try again.