Allow passing default value stubs to mock initialization

This commit is contained in:
Michael Miller 2022-06-01 22:04:18 -06:00
parent 225553127d
commit 3f4216a271
No known key found for this signature in database
GPG key ID: 32B47AE8F388A1FF
2 changed files with 28 additions and 1 deletions

View file

@ -174,6 +174,28 @@ Spectator.describe "Mock DSL", :smoke do
fake.method8(1, 2, 3, kwarg: 4, x: 5, y: 6, z: 7) { :block }
expect(fake._spectator_calls).to contain_exactly(:method3, :method6, :method7, :method8)
end
# Cannot test unexpected messages - will not compile due to missing methods.
describe "deferred default stubs" do
mock(ConcreteClass)
let(fake2) do
mock(ConcreteClass,
method1: "stubbed",
method3: 123,
method4: :xyz)
end
it "uses the keyword arguments as stubs" do
aggregate_failures do
expect(fake2.method1).to eq("stubbed")
expect(fake2.method2).to eq(:original)
expect(fake2.method3(42)).to eq(123)
expect(fake2.method4 { :foo }).to eq(:xyz)
end
end
end
end
context "with an abstract class" do

View file

@ -178,7 +178,12 @@ module Spectator::DSL
found_tuple = found_tuples.last %}
{% if found_tuple %}
{{found_tuple[2].id}}.new({{**value_methods}})
{{found_tuple[2].id}}.new.tap do |mock|
{% for key, value in value_methods %}
%stub{key} = ::Spectator::ValueStub.new({{key.id.symbolize}}, {{value}})
mock._spectator_define_stub(%stub{key})
{% end %}
end
{% else %}
{% raise "Type `#{type.id}` must be previously mocked before attempting to instantiate." %}
{% end %}