Skip to content

Instantly share code, notes, and snippets.

@davetron5000
Created March 9, 2021 14:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davetron5000/62ea68c47bdbf3852f3a6c5f6dc39f5c to your computer and use it in GitHub Desktop.
Save davetron5000/62ea68c47bdbf3852f3a6c5f6dc39f5c to your computer and use it in GitHub Desktop.
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