forked from rspec/rspec-mocks
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
clean up and enhance spec for mixing stubs and expectations
- Loading branch information
1 parent
4c32db1
commit 117f2cb
Showing
1 changed file
with
38 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,47 @@ | ||
require 'spec_helper' | ||
|
||
module RSpec | ||
module Mocks | ||
describe "Example with stubbed and then called message" do | ||
it "fails if the message is expected and then subsequently not called again" do | ||
double = double("mock", :msg => nil) | ||
double.msg | ||
double.should_receive(:msg) | ||
lambda { double.rspec_verify }.should raise_error(RSpec::Mocks::MockExpectationError) | ||
end | ||
describe "expection set on previously stubbed method" do | ||
it "fails if message is not received after expectation is set" do | ||
double = double(:msg => nil) | ||
double.msg | ||
double.should_receive(:msg) | ||
lambda { double.rspec_verify }.should raise_error(RSpec::Mocks::MockExpectationError) | ||
end | ||
|
||
it "outputs arguments of similar calls" do | ||
double = double('double', :foo => true) | ||
double.should_receive(:foo).with('first') | ||
double.foo('second') | ||
double.foo('third') | ||
lambda do | ||
double.rspec_verify | ||
end.should raise_error(%Q|Double "double" received :foo with unexpected arguments\n expected: ("first")\n got: ("second"), ("third")|) | ||
double.rspec_reset | ||
end | ||
|
||
it "outputs arguments of all similar calls" do | ||
double = double('double', :foo => true) | ||
double.should_receive(:foo).with('first') | ||
double.foo('second') | ||
double.foo('third') | ||
lambda do | ||
double.rspec_verify | ||
end.should raise_error(%Q|Double "double" received :foo with unexpected arguments\n expected: ("first")\n got: ("second"), ("third")|) | ||
double.rspec_reset | ||
end | ||
context "with argument constraint on stub" do | ||
it "matches any args if no arg constraint set on expectation" do | ||
double = double("mock") | ||
double.stub(:foo).with(3).and_return("stub") | ||
double.should_receive(:foo).at_least(:once).and_return("expectation") | ||
double.foo | ||
double.rspec_verify | ||
end | ||
|
||
describe "Example with stubbed with args and expectation with no args" do | ||
it "matches any args even if previously stubbed with arguments" do | ||
double = double("mock") | ||
double.stub(:foo).with(3).and_return("stub") | ||
double.should_receive(:foo).at_least(:once).and_return("expectation") | ||
double.foo | ||
double.rspec_verify | ||
end | ||
it "matches specific args set on expectation" do | ||
double = double("mock") | ||
double.stub(:foo).with(3).and_return("stub") | ||
double.should_receive(:foo).at_least(:once).with(4).and_return("expectation") | ||
double.foo(4) | ||
double.rspec_verify | ||
end | ||
|
||
it "fails if expectation's arg constraint is not met" do | ||
double = double("mock") | ||
double.stub(:foo).with(3).and_return("stub") | ||
double.should_receive(:foo).at_least(:once).with(4).and_return("expectation") | ||
double.foo(3) | ||
expect { double.rspec_verify }.to raise_error(/expected: \(4\)\s+got: \(3\)/) | ||
end | ||
end | ||
end |