| 1 |
require File.dirname(__FILE__) + '/../../spec_helper.rb' |
| 2 |
|
| 3 |
module Spec |
| 4 |
module Mocks |
| 5 |
describe "using a Partial Mock," do |
| 6 |
before(:each) do |
| 7 |
@object = Object.new |
| 8 |
end |
| 9 |
|
| 10 |
it "should name the class in the failure message" do |
| 11 |
@object.should_receive(:foo) |
| 12 |
lambda do |
| 13 |
@object.rspec_verify |
| 14 |
end.should raise_error(Spec::Mocks::MockExpectationError, /Object/) |
| 15 |
end |
| 16 |
|
| 17 |
it "should not conflict with @options in the object" do |
| 18 |
@object.instance_eval { @options = Object.new } |
| 19 |
@object.should_receive(:blah) |
| 20 |
@object.blah |
| 21 |
end |
| 22 |
|
| 23 |
it "should_not_receive should mock out the method" do |
| 24 |
pending("example raises the expected error, yet fails") |
| 25 |
@object.should_not_receive(:fuhbar) |
| 26 |
lambda do |
| 27 |
@object.fuhbar |
| 28 |
end.should raise_error(MockExpectationError, "Mock 'Object' expected :fuhbar with (no args) 0 times, but received it once") |
| 29 |
end |
| 30 |
|
| 31 |
it "should_not_receive should return a negative message expectation" do |
| 32 |
@object.should_not_receive(:foobar).should be_kind_of(NegativeMessageExpectation) |
| 33 |
end |
| 34 |
|
| 35 |
it "should_receive should mock out the method" do |
| 36 |
@object.should_receive(:foobar).with(:test_param).and_return(1) |
| 37 |
@object.foobar(:test_param).should equal(1) |
| 38 |
end |
| 39 |
|
| 40 |
it "should_receive should handle a hash" do |
| 41 |
@object.should_receive(:foobar).with(:key => "value").and_return(1) |
| 42 |
@object.foobar(:key => "value").should equal(1) |
| 43 |
end |
| 44 |
|
| 45 |
it "should_receive should handle an inner hash" do |
| 46 |
hash = {:a => {:key => "value"}} |
| 47 |
@object.should_receive(:foobar).with(:key => "value").and_return(1) |
| 48 |
@object.foobar(hash[:a]).should equal(1) |
| 49 |
end |
| 50 |
|
| 51 |
it "should_receive should return a message expectation" do |
| 52 |
@object.should_receive(:foobar).should be_kind_of(MessageExpectation) |
| 53 |
@object.foobar |
| 54 |
end |
| 55 |
|
| 56 |
it "should_receive should verify method was called" do |
| 57 |
@object.should_receive(:foobar).with(:test_param).and_return(1) |
| 58 |
lambda do |
| 59 |
@object.rspec_verify |
| 60 |
end.should raise_error(Spec::Mocks::MockExpectationError) |
| 61 |
end |
| 62 |
|
| 63 |
it "should_receive should also take a String argument" do |
| 64 |
@object.should_receive('foobar') |
| 65 |
@object.foobar |
| 66 |
end |
| 67 |
|
| 68 |
it "should_not_receive should also take a String argument" do |
| 69 |
pending("example raises the expected error, yet fails") |
| 70 |
@object.should_not_receive('foobar') |
| 71 |
lambda do |
| 72 |
@object.foobar |
| 73 |
end.should raise_error(Spec::Mocks::MockExpectationError) |
| 74 |
end |
| 75 |
|
| 76 |
it "should use report nil in the error message" do |
| 77 |
@this_will_resolve_to_nil.should_receive(:foobar) |
| 78 |
lambda do |
| 79 |
@this_will_resolve_to_nil.rspec_verify |
| 80 |
end.should raise_error(Spec::Mocks::MockExpectationError, /NilClass.*expected :foobar with/) |
| 81 |
end |
| 82 |
end |
| 83 |
|
| 84 |
describe "Partially mocking an object that defines ==, after another mock has been defined" do |
| 85 |
before(:each) do |
| 86 |
stub("existing mock", :foo => :foo) |
| 87 |
end |
| 88 |
|
| 89 |
class PartiallyMockedEquals |
| 90 |
attr_reader :val |
| 91 |
def initialize(val) |
| 92 |
@val = val |
| 93 |
end |
| 94 |
|
| 95 |
def ==(other) |
| 96 |
@val == other.val |
| 97 |
end |
| 98 |
end |
| 99 |
|
| 100 |
it "should not raise an error when stubbing the object" do |
| 101 |
o = PartiallyMockedEquals.new :foo |
| 102 |
lambda { o.stub!(:bar) }.should_not raise_error(NoMethodError) |
| 103 |
end |
| 104 |
end |
| 105 |
end |
| 106 |
end |