Use new mock DSL

This commit is contained in:
Michael Miller 2024-05-16 21:22:11 -06:00
parent 2bb9fe8dcd
commit 4020bad725
No known key found for this signature in database
GPG key ID: AC78B32D30CE34A2
3 changed files with 47 additions and 45 deletions

View file

@ -2,21 +2,21 @@ require "../../../spec_helper"
Spectator.describe "Allow stub DSL" do
context "with a double" do
double(:dbl) do
double Double do
# Ensure the original is never called.
stub abstract def foo : Nil
stub abstract def foo(arg) : Nil
stub abstract def value : Int32
end
let(dbl) { double(:dbl) }
let(dbl) { Double.new }
# Ensure invocations don't leak between examples.
pre_condition { expect(dbl).to_not have_received(:foo), "Leaked method calls from previous examples" }
# Ensure stubs don't leak between examples.
pre_condition do
expect { dbl.foo }.to raise_error(Spectator::UnexpectedMessage)
expect { dbl.foo }.to raise_error(Mocks::UnexpectedMessage)
end
it "matches when a message is received" do
@ -36,37 +36,39 @@ Spectator.describe "Allow stub DSL" do
it "raises when a message without arguments is received" do
allow(dbl).to receive(:foo).with(:bar)
expect { dbl.foo }.to raise_error(Spectator::UnexpectedMessage, /foo/)
expect { dbl.foo }.to raise_error(Mocks::UnexpectedMessage, /foo/)
end
it "raises when a message with different arguments is received" do
allow(dbl).to receive(:foo).with(:baz)
expect { dbl.foo(:bar) }.to raise_error(Spectator::UnexpectedMessage, /foo/)
expect { dbl.foo(:bar) }.to raise_error(Mocks::UnexpectedMessage, /foo/)
end
end
context "with a class double" do
double(:dbl) do
double Double do
# Ensure the original is never called.
stub abstract def self.foo : Nil
stub def self.foo : Nil
raise "Original method called"
end
stub abstract def self.foo(arg) : Nil
stub def self.foo(arg) : Nil
raise "Original method called #{arg}"
end
stub abstract def self.value : Int32
stub def self.value : Int32
42
end
end
let(dbl) { class_double(:dbl) }
let(dbl) { Double }
# Ensure invocations don't leak between examples.
pre_condition { expect(dbl).to_not have_received(:foo), "Leaked method calls from previous examples" }
# Ensure stubs don't leak between examples.
pre_condition do
expect { dbl.foo }.to raise_error(Spectator::UnexpectedMessage)
expect { dbl.foo }.to raise_error("Original method called")
end
it "matches when a message is received" do
@ -84,14 +86,14 @@ Spectator.describe "Allow stub DSL" do
expect { dbl.foo(:bar) }.to_not raise_error
end
it "raises when a message without arguments is received" do
it "calls the original without arguments is received" do
allow(dbl).to receive(:foo).with(:bar)
expect { dbl.foo }.to raise_error(Spectator::UnexpectedMessage, /foo/)
expect { dbl.foo }.to raise_error("Original method called")
end
it "raises when a message with different arguments is received" do
it "calls the original with different arguments is received" do
allow(dbl).to receive(:foo).with(:baz)
expect { dbl.foo(:bar) }.to raise_error(Spectator::UnexpectedMessage, /foo/)
expect { dbl.foo(:bar) }.to raise_error("Original method called bar")
end
end
@ -101,16 +103,16 @@ Spectator.describe "Allow stub DSL" do
abstract def foo(arg) : Int32
end
mock(MyClass)
mock MyClassMock < MyClass
let(fake) { mock(MyClass) }
let(fake) { MyClassMock.new }
# Ensure invocations don't leak between examples.
pre_condition { expect(fake).to_not have_received(:foo), "Leaked method calls from previous examples" }
# Ensure stubs don't leak between examples.
pre_condition do
expect { fake.foo }.to raise_error(Spectator::UnexpectedMessage)
expect { fake.foo }.to raise_error(Mocks::UnexpectedMessage)
end
it "matches when a message is received" do
@ -130,12 +132,12 @@ Spectator.describe "Allow stub DSL" do
it "raises when a message without arguments is received" do
allow(fake).to receive(:foo).with(:bar)
expect { fake.foo }.to raise_error(Spectator::UnexpectedMessage, /foo/)
expect { fake.foo }.to raise_error(Mocks::UnexpectedMessage, /foo/)
end
it "raises when a message with different arguments is received" do
allow(fake).to receive(:foo).with(:baz)
expect { fake.foo(:bar) }.to raise_error(Spectator::UnexpectedMessage, /foo/)
expect { fake.foo(:bar) }.to raise_error(Mocks::UnexpectedMessage, /foo/)
end
end
@ -150,15 +152,15 @@ Spectator.describe "Allow stub DSL" do
end
end
mock(MyClass)
mock MyClassMock < MyClass
let(fake) { class_mock(MyClass) }
let(fake) { MyClassMock }
# Ensure invocations don't leak between examples.
pre_condition { expect(fake).to_not have_received(:foo), "Leaked method calls from previous examples" }
# Ensure stubs don't leak between examples.
pre_condition { expect(fake.foo).to eq(42) }
pre_condition { expect { fake.foo }.to raise_error(Mocks::UnexpectedMessage, /foo/) }
it "matches when a message is received" do
allow(fake).to receive(:foo).and_return(0)
@ -175,14 +177,14 @@ Spectator.describe "Allow stub DSL" do
expect(fake.foo(:bar)).to eq(0)
end
it "calls the original when a message without arguments is received" do
it "raises when a message without arguments is received" do
allow(fake).to receive(:foo).with(:bar)
expect(fake.foo).to eq(42)
expect { fake.foo }.to raise_error(Mocks::UnexpectedMessage, /foo/)
end
it "calls the original when a message with different arguments is received" do
it "raises when a message with different arguments is received" do
allow(fake).to receive(:foo).with(:baz)
expect(fake.foo(:bar)).to eq(42)
expect { fake.foo(:bar) }.to raise_error(Mocks::UnexpectedMessage, /foo/)
end
end
end

View file

@ -2,8 +2,8 @@ require "../../../spec_helper"
Spectator.describe "Null double DSL" do
context "specifying methods as keyword args" do
double(:test, foo: "foobar", bar: 42)
subject(dbl) { double(:test).as_null_object }
double TestDouble, foo: "foobar", bar: 42
subject(dbl) { TestDouble.new.as_null_object }
it "defines a double with methods" do
aggregate_failures do
@ -46,13 +46,13 @@ Spectator.describe "Null double DSL" do
context "block with stubs" do
context "one method" do
double(:test2) do
double TestDouble2 do
stub def foo
"one method"
end
end
subject(dbl) { double(:test2).as_null_object }
subject(dbl) { TestDouble2.new.as_null_object }
it "defines a double with methods" do
expect(dbl.foo).to eq("one method")
@ -64,7 +64,7 @@ Spectator.describe "Null double DSL" do
end
context "two methods" do
double(:test3) do
double TestDouble3 do
stub def foo
"two methods"
end
@ -74,7 +74,7 @@ Spectator.describe "Null double DSL" do
end
end
subject(dbl) { double(:test3).as_null_object }
subject(dbl) { TestDouble3.new.as_null_object }
it "defines a double with methods" do
aggregate_failures do
@ -92,10 +92,10 @@ Spectator.describe "Null double DSL" do
end
context "empty block" do
double(:test4) do
double TestDouble4 do
end
subject(dbl) { double(:test4).as_null_object }
subject(dbl) { TestDouble4.new.as_null_object }
it "defines a double" do
expect(dbl).to be_a(Spectator::Double)
@ -103,13 +103,13 @@ Spectator.describe "Null double DSL" do
end
context "stub-less method" do
double(:test5) do
double TestDouble5 do
def foo
"no stub"
end
end
subject(dbl) { double(:test5).as_null_object }
subject(dbl) { TestDouble5.new.as_null_object }
it "defines a double with methods" do
expect(dbl.foo).to eq("no stub")
@ -117,7 +117,7 @@ Spectator.describe "Null double DSL" do
end
context "mixing keyword arguments" do
double(:test6, foo: "kwargs", bar: 42) do
double(TestDouble6, foo: "kwargs", bar: 42) do
stub def foo
"block"
end
@ -131,7 +131,7 @@ Spectator.describe "Null double DSL" do
end
end
subject(dbl) { double(:test6).as_null_object }
subject(dbl) { TestDouble6.new.as_null_object }
it "overrides the keyword arguments with the block methods" do
expect(dbl.foo).to eq("block")
@ -155,7 +155,7 @@ Spectator.describe "Null double DSL" do
end
context "methods accepting blocks" do
double(:test7) do
double TestDouble7 do
stub def foo(&)
yield
end
@ -165,7 +165,7 @@ Spectator.describe "Null double DSL" do
end
end
subject(dbl) { double(:test7).as_null_object }
subject(dbl) { TestDouble7.new.as_null_object }
it "defines the method and yields" do
expect(dbl.foo { :xyz }).to eq(:xyz)
@ -178,9 +178,9 @@ Spectator.describe "Null double DSL" do
end
describe "predefined method stubs" do
double(:test8, foo: 42)
double(TestDouble8, foo: 42)
let(dbl) { double(:test8, foo: 7).as_null_object }
let(dbl) { TestDouble8.new(foo: 7).as_null_object }
it "overrides the original value" do
expect(dbl.foo).to eq(7)

View file

@ -1,12 +1,12 @@
require "../../../spec_helper"
Spectator.describe "Stub DSL", :smoke do
double(:foobar, foo: 42, bar: "baz") do
double(FoobarDouble, foo: 42, bar: "baz") do
stub abstract def other : String
stub abstract def null : Nil
end
let(dbl) { double(:foobar) }
let(dbl) { FoobarDouble.new }
it "overrides default stubs" do
allow(dbl).to receive(:foo).and_return(123)