Last active
August 29, 2015 14:07
-
-
Save plukevdh/7438f96ec0bd4dd65de8 to your computer and use it in GitHub Desktop.
Why do mocked objects not assume identity of the object type they're mocking?
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
$> rspec test_case.rb | |
F. | |
Failures: | |
1) a failing case expects success | |
Failure/Error: expect(production_code(response)).to eq 'success' | |
expected: "success" | |
got: "failure" | |
(compared using ==) | |
# ./test_case.rb:28:in `block (2 levels) in <top (required)>' | |
Finished in 0.00106 seconds (files took 0.35682 seconds to load) | |
2 examples, 1 failure | |
Failed examples: | |
rspec ./test_case.rb:26 # a failing case expects success |
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
require 'rspec' | |
module MyRequest | |
Good = Class.new | |
Redirect = Class.new | |
Fail = Class.new | |
end | |
def production_code(response) | |
case response | |
when MyRequest::Good | |
'success' | |
when MyRequest::Redirect, # 301 | |
'redirect' | |
else | |
'failure' | |
end | |
end | |
RSpec.configure do |config| | |
config.mock_with :rspec do |mocks| | |
mocks.verify_partial_doubles = true | |
mocks.verify_doubled_constant_names = true | |
end | |
end | |
RSpec.describe "a failing case" do | |
it "expects success" do | |
response = instance_double('MyRequest::Good') | |
expect(production_code(response)).to eq 'success' | |
end | |
it 'expects failure' do | |
response = instance_double('MyRequest::Fail') | |
expect(production_code(response)).to eq 'failure' | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Fixed typo, added requires.