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
|
require 'spec_helper'
module RSpec::Mocks
describe "PartialMockUsingMocksDirectly" do
let(:klass) do
Class.new do
module MethodMissing
remove_method :method_missing rescue nil
def method_missing(m, *a, &b)
if m == :captured_by_method_missing
"response generated by method missing"
else
super(m, *a, &b)
end
end
end
extend MethodMissing
include MethodMissing
def existing_method
:original_value
end
end
end
let(:obj) { klass.new }
# See http://rubyforge.org/tracker/index.php?func=detail&aid=10263&group_id=797&atid=3149
# specify "should clear expectations on verify" do
# obj.should_receive(:msg)
# obj.msg
# verify obj
# expect {
# obj.msg
# }.to raise_error(NoMethodError)
#
# end
it "fails when expected message is not received" do
obj.should_receive(:msg)
expect {
verify obj
}.to raise_error(RSpec::Mocks::MockExpectationError)
end
it "fails when message is received with incorrect args" do
obj.should_receive(:msg).with(:correct_arg)
expect {
obj.msg(:incorrect_arg)
}.to raise_error(RSpec::Mocks::MockExpectationError)
obj.msg(:correct_arg)
end
it "passes when expected message is received" do
obj.should_receive(:msg)
obj.msg
verify obj
end
it "passes when message is received with correct args" do
obj.should_receive(:msg).with(:correct_arg)
obj.msg(:correct_arg)
verify obj
end
it "restores the original method if it existed" do
expect(obj.existing_method).to equal(:original_value)
obj.should_receive(:existing_method).and_return(:mock_value)
expect(obj.existing_method).to equal(:mock_value)
verify obj
expect(obj.existing_method).to equal(:original_value)
end
context "with an instance method handled by method_missing" do
it "restores the original behavior" do
expect(obj.captured_by_method_missing).to eq("response generated by method missing")
obj.stub(:captured_by_method_missing) { "foo" }
expect(obj.captured_by_method_missing).to eq("foo")
reset obj
expect(obj.captured_by_method_missing).to eq("response generated by method missing")
end
end
context "with a class method handled by method_missing" do
it "restores the original behavior" do
expect(klass.captured_by_method_missing).to eq("response generated by method missing")
klass.stub(:captured_by_method_missing) { "foo" }
expect(klass.captured_by_method_missing).to eq("foo")
reset klass
expect(klass.captured_by_method_missing).to eq("response generated by method missing")
end
end
end
end
|