Permalink
Browse files

Add new feature for receive_counts on mocks

  • Loading branch information...
1 parent cfa7a96 commit 52daacc296ac78f49d99baf2f5c90abbda4aaac3 @adomokos committed Aug 26, 2011
Showing with 212 additions and 0 deletions.
  1. +212 −0 features/message_expectations/receive_counts.feature
View
212 features/message_expectations/receive_counts.feature
@@ -0,0 +1,212 @@
+Feature: receive counts
+
+ The implicit expectation is that the message passed to should_receive()
+ will be called x number of times.
+
+ 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"

0 comments on commit 52daacc

Please sign in to comment.