Created
March 9, 2021 14:59
-
-
Save davetron5000/62ea68c47bdbf3852f3a6c5f6dc39f5c to your computer and use it in GitHub Desktop.
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" | |
RSpec.configure do |config| | |
config.expect_with :rspec do |expectations| | |
expectations.include_chain_clauses_in_custom_matcher_descriptions = true | |
end | |
config.mock_with :rspec do |mocks| | |
mocks.verify_partial_doubles = true | |
end | |
end | |
class ClassWeMock | |
def mocked_method(a: , b: ) | |
a + b | |
end | |
end | |
class UsesKwargs | |
def doit(foo) | |
bar = ClassWeMock.new.mocked_method(a: foo, b: 10) | |
bar + 1 | |
end | |
end | |
class UsesHash | |
def doit(foo) | |
hash = { a: foo, b: 10 } | |
bar = ClassWeMock.new.mocked_method(hash) | |
bar + 1 | |
end | |
end | |
RSpec.describe UsesKwargs do | |
subject(:uses_kwargs) { described_class.new } | |
describe "#doit" do | |
it "calls into ClassWeMock" do | |
class_we_mock = instance_double(ClassWeMock) | |
allow(ClassWeMock).to receive(:new).and_return(class_we_mock) | |
allow(class_we_mock).to receive(:mocked_method).and_return(99) | |
result = uses_kwargs.doit(20) | |
expect(result).to eq(100) | |
expect(class_we_mock).to have_received(:mocked_method).with(a: 20, b: 10) | |
end | |
end | |
end | |
RSpec.describe UsesHash do | |
subject(:uses_kwargs) { described_class.new } | |
describe "#doit" do | |
it "calls into ClassWeMock" do | |
class_we_mock = instance_double(ClassWeMock) | |
allow(ClassWeMock).to receive(:new).and_return(class_we_mock) | |
allow(class_we_mock).to receive(:mocked_method).and_return(99) | |
result = uses_kwargs.doit(20) | |
expect(result).to eq(100) | |
expect(class_we_mock).to have_received(:mocked_method).with(a: 20, b: 10) | |
end | |
end | |
end | |
if ARGV[0] == "run" | |
puts "UsesKwargs" | |
puts UsesKwargs.new.doit(20) | |
puts "UsesHash" | |
begin | |
puts UsesHash.new.doit(20) | |
rescue ArgumentError => ex | |
puts ex.message | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment