mirror of
https://gitea.invidious.io/iv-org/shard-spectator.git
synced 2024-08-15 00:53:35 +00:00
Fix stub_type when used on a sub-type
previous_def should only be used if the type being stubbed matches the type stub methods are defined in. Re-enable tests for define_subclass mocking.
This commit is contained in:
parent
86b49dc20e
commit
1adcf74c3f
2 changed files with 42 additions and 39 deletions
|
@ -15,55 +15,58 @@ class Thing2
|
||||||
end
|
end
|
||||||
|
|
||||||
Spectator.describe Spectator::Mock do
|
Spectator.describe Spectator::Mock do
|
||||||
# describe "#define_subclass" do
|
describe "#define_subclass" do
|
||||||
# class Thing
|
class Thing
|
||||||
# def method1
|
def method1
|
||||||
# 42
|
42
|
||||||
# end
|
end
|
||||||
|
|
||||||
# def method2
|
def method2
|
||||||
# :original
|
:original
|
||||||
# end
|
end
|
||||||
|
|
||||||
# def method3
|
def method3
|
||||||
# "original"
|
"original"
|
||||||
# end
|
end
|
||||||
# end
|
end
|
||||||
|
|
||||||
# Spectator::Mock.define_subclass(Thing, MockThing, :mock_name, method1: 123) do
|
Spectator::Mock.define_subclass(Thing, MockThing, :mock_name, method1: 123) do
|
||||||
# stub def method2
|
stub def method2
|
||||||
# :stubbed
|
:stubbed
|
||||||
# end
|
end
|
||||||
# end
|
end
|
||||||
|
|
||||||
# let(thing) { MockThing.new }
|
let(thing) { MockThing.new }
|
||||||
|
|
||||||
# it "defines a subclass of the mocked type" do
|
it "defines a subclass of the mocked type" do
|
||||||
# expect(MockThing).to be_lt(Thing)
|
expect(MockThing).to be_lt(Thing)
|
||||||
# end
|
end
|
||||||
|
|
||||||
# it "overrides responses from methods with keyword arguments" do
|
it "overrides responses from methods with keyword arguments" do
|
||||||
# expect(thing.method1).to eq(123)
|
expect(thing.method1).to eq(123)
|
||||||
# end
|
end
|
||||||
|
|
||||||
# it "overrides responses from methods defined in the block" do
|
it "overrides responses from methods defined in the block" do
|
||||||
# expect(thing.method2).to eq(:stubbed)
|
expect(thing.method2).to eq(:stubbed)
|
||||||
# end
|
end
|
||||||
|
|
||||||
# it "allows methods to be stubbed" do
|
it "allows methods to be stubbed" do
|
||||||
# stub1 = Spectator::ValueStub.new(:method1, 777)
|
stub1 = Spectator::ValueStub.new(:method1, 777)
|
||||||
# stub2 = Spectator::ValueStub.new(:method2, :override)
|
stub2 = Spectator::ValueStub.new(:method2, :override)
|
||||||
# stub3 = Spectator::ValueStub.new(:method3, "stubbed")
|
stub3 = Spectator::ValueStub.new(:method3, "stubbed")
|
||||||
|
|
||||||
# aggregate_failures do
|
aggregate_failures do
|
||||||
# expect { thing._spectator_define_stub(stub1) }.to change { thing.method1 }.to(777)
|
expect { thing._spectator_define_stub(stub1) }.to change { thing.method1 }.to(777)
|
||||||
# expect { thing._spectator_define_stub(stub2) }.to change { thing.method2 }.to(:override)
|
expect { thing._spectator_define_stub(stub2) }.to change { thing.method2 }.to(:override)
|
||||||
# expect { thing._spectator_define_stub(stub3) }.to change { thing.method3 }.from("original").to("stubbed")
|
expect { thing._spectator_define_stub(stub3) }.to change { thing.method3 }.from("original").to("stubbed")
|
||||||
# end
|
end
|
||||||
# end
|
end
|
||||||
# end
|
end
|
||||||
|
|
||||||
describe "#inject" do
|
describe "#inject" do
|
||||||
|
# For some reason, Thing2 is not visible to `inject` below when defined here.
|
||||||
|
# Thing2's definition is outside of the spec to get around this.
|
||||||
|
|
||||||
context "with a class" do
|
context "with a class" do
|
||||||
Spectator::Mock.inject(Thing2, :mock_name, method1: 123) do
|
Spectator::Mock.inject(Thing2, :mock_name, method1: 123) do
|
||||||
stub def method2
|
stub def method2
|
||||||
|
|
|
@ -318,7 +318,7 @@ module Spectator
|
||||||
{% if method.double_splat %}**{{method.double_splat}}, {% end %}
|
{% if method.double_splat %}**{{method.double_splat}}, {% end %}
|
||||||
{% if method.block_arg %}&{{method.block_arg}}{% elsif method.accepts_block? %}&{% end %}
|
{% if method.block_arg %}&{{method.block_arg}}{% elsif method.accepts_block? %}&{% end %}
|
||||||
){% if method.return_type %} : {{method.return_type}}{% end %}{% if !method.free_vars.empty? %} forall {{method.free_vars.splat}}{% end %}
|
){% if method.return_type %} : {{method.return_type}}{% end %}{% if !method.free_vars.empty? %} forall {{method.free_vars.splat}}{% end %}
|
||||||
previous_def{% if method.accepts_block? %} { |*%yargs| yield *%yargs }{% end %}
|
{% if type == @type %}previous_def{% else %}super{% end %}{% if method.accepts_block? %} { |*%yargs| yield *%yargs }{% end %}
|
||||||
end
|
end
|
||||||
{% end %}
|
{% end %}
|
||||||
end
|
end
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue