Remove or skip mock, double, and stub tests

This commit is contained in:
Michael Miller 2022-03-19 17:20:15 -06:00
parent 0c7f72dc78
commit b83c6b7b1e
No known key found for this signature in database
GPG key ID: 32B47AE8F388A1FF
10 changed files with 30 additions and 268 deletions

View file

@ -1,32 +0,0 @@
require "../../spec_helper"
class Phonebook
def find(name)
# Some expensive lookup call.
"+18005554321"
end
end
class Resolver
def initialize(@phonebook : Phonebook)
end
def find(name)
@phonebook.find(name)
end
end
Spectator.describe Resolver do
mock Phonebook do
stub find(name)
end
describe "#find" do
it "can find number" do
pb = Phonebook.new
allow(pb).to receive(find).and_return("+18005551234")
resolver = Resolver.new(pb)
expect(resolver.find("Bob")).to eq("+18005551234")
end
end
end

View file

@ -1,40 +0,0 @@
require "../../spec_helper"
Spectator.describe "Doubles" do
double :my_double do
stub answer { 42 }
end
specify "the answer to everything" do
dbl = double(:my_double)
expect(dbl.answer).to eq(42)
end
end
class MyType
def answer
123
end
end
Spectator.describe "Mocks" do
mock MyType do
stub answer { 42 }
end
specify "the answer to everything" do
m = MyType.new
expect(m.answer).to eq(42)
end
end
Spectator.describe "Mocks and doubles" do
double :my_double do
stub answer : Int32 # Return type required, otherwise nil is assumed.
end
specify "the answer to everything" do
dbl = double(:my_double)
allow(dbl).to receive(:answer).and_return(42)
expect(dbl.answer).to eq(42)
end
end

View file

@ -1,166 +0,0 @@
require "../../spec_helper"
Spectator.describe "Stubs" do
context "Implementing a Stub" do
double :my_double do
stub answer : Int32
stub do_something
end
it "knows the answer" do
dbl = double(:my_double)
allow(dbl).to receive(:answer).and_return(42)
expect(dbl.answer).to eq(42)
end
it "does something" do
dbl = double(:my_double)
allow(dbl).to receive(:do_something)
expect(dbl.do_something).to be_nil
end
context "and_return" do
double :my_double do
stub to_s : String
stub do_something
end
it "stringifies" do
dbl = double(:my_double)
allow(dbl).to receive(:to_s).and_return("foobar")
expect(dbl.to_s).to eq("foobar")
end
it "returns gibberish" do
dbl = double(:my_double)
allow(dbl).to receive(:to_s).and_return("foo", "bar", "baz")
expect(dbl.to_s).to eq("foo")
expect(dbl.to_s).to eq("bar")
expect(dbl.to_s).to eq("baz")
expect(dbl.to_s).to eq("baz")
end
it "returns nil" do
dbl = double(:my_double)
allow(dbl).to receive(:do_something).and_return
expect(dbl.do_something).to be_nil
end
end
context "and_raise" do
double :my_double do
stub oops
end
it "raises an error" do
dbl = double(:my_double)
allow(dbl).to receive(:oops).and_raise(DivisionByZeroError.new)
expect { dbl.oops }.to raise_error(DivisionByZeroError)
end
it "raises an error" do
dbl = double(:my_double)
allow(dbl).to receive(:oops).and_raise("Something broke")
expect { dbl.oops }.to raise_error(/Something broke/)
end
it "raises an error" do
dbl = double(:my_double)
allow(dbl).to receive(:oops).and_raise(ArgumentError, "Size must be > 0")
expect { dbl.oops }.to raise_error(ArgumentError, /Size/)
end
end
context "and_call_original" do
class MyType
def foo
"foo"
end
end
mock MyType do
stub foo
end
it "calls the original" do
instance = MyType.new
allow(instance).to receive(:foo).and_call_original
expect(instance.foo).to eq("foo")
end
end
context "Short-hand for Multiple Stubs" do
double :my_double do
stub method_a : Symbol
stub method_b : Int32
stub method_c : String
end
it "does something" do
dbl = double(:my_double)
allow(dbl).to receive_messages(method_a: :foo, method_b: 42, method_c: "foobar")
expect(dbl.method_a).to eq(:foo)
expect(dbl.method_b).to eq(42)
expect(dbl.method_c).to eq("foobar")
end
end
context "Custom Implementation" do
double :my_double do
stub foo : String
end
it "does something" do
dbl = double(:my_double)
allow(dbl).to receive(:foo) { "foo" }
expect(dbl.foo).to eq("foo")
end
end
context "Arguments" do
double :my_double do
stub add(a, b) { a + b }
stub do_something(arg) { arg } # Return the argument by default.
end
it "adds two numbers" do
dbl = double(:my_double)
allow(dbl).to receive(:add).and_return(7)
expect(dbl.add(1, 2)).to eq(7)
end
it "does basic matching" do
dbl = double(:my_double)
allow(dbl).to receive(:do_something).with(1).and_return(42)
allow(dbl).to receive(:do_something).with(2).and_return(22)
expect(dbl.do_something(1)).to eq(42)
expect(dbl.do_something(2)).to eq(22)
end
it "can call the original" do
dbl = double(:my_double)
allow(dbl).to receive(:do_something).with(1).and_return(42)
allow(dbl).to receive(:do_something).with(2).and_call_original
expect(dbl.do_something(1)).to eq(42)
expect(dbl.do_something(2)).to eq(2)
end
it "falls back to the default" do
dbl = double(:my_double)
allow(dbl).to receive(:do_something).and_return(22)
allow(dbl).to receive(:do_something).with(1).and_return(42)
expect(dbl.do_something(1)).to eq(42)
expect(dbl.do_something(2)).to eq(22)
expect(dbl.do_something(3)).to eq(22)
end
it "does advanced matching" do
dbl = double(:my_double)
allow(dbl).to receive(:do_something).with(Int32).and_return(42)
allow(dbl).to receive(:do_something).with(String).and_return("foobar")
allow(dbl).to receive(:do_something).with(/hi/).and_return("hello there")
expect(dbl.do_something(1)).to eq(42)
expect(dbl.do_something("foo")).to eq("foobar")
expect(dbl.do_something("hi there")).to eq("hello there")
end
end
end
end

View file

@ -7,11 +7,9 @@ Spectator.describe "GitHub Issue #28" do
end
end
mock Test do
stub foo
end
# mock Test
it "matches method stubs with no_args" do
xit "matches method stubs with no_args", pending: "Mock redesign" do
test = Test.new
expect(test).to receive(:foo).with(no_args).and_return(42)
test.foo

View file

@ -7,12 +7,12 @@ Spectator.describe "GitHub Issue #29" do
end
end
mock SomeClass do
stub exit(code)
end
# mock SomeClass do
# inject_stub abstract def exit(code)
# end
describe SomeClass do
it "captures exit" do
xit "captures exit", pending: "Mock redesign" do
expect(subject).to receive(:exit).with(0)
subject.goodbye
end
@ -25,13 +25,13 @@ Spectator.describe "GitHub Issue #29" do
end
end
mock Foo do
stub self.exit(code)
end
# mock Foo do
# inject_stub abstract def self.exit(code)
# end
subject { Foo }
it "must capture exit" do
xit "must capture exit", pending: "Mock redesign" do
expect(subject).to receive(:exit).with(0)
subject.test

View file

@ -1,9 +1,9 @@
require "../spec_helper"
Spectator.describe "GitHub Issue #30" do
let(dbl) { double(:foo) }
# let(dbl) { double(:foo) }
it "supports block-less symbol doubles" do
xit "supports block-less symbol doubles", pending: "Mock redesign" do
expect(dbl).to_not be_nil
end
end

View file

@ -21,12 +21,12 @@ Spectator.describe "GitHub Issue #32" do
let(test_instance) { test_class.new }
describe "something else" do
mock TestFoo::TestClass do
stub self.new
stub test
end
# mock TestFoo::TestClass do
# stub self.new
# stub test
# end
it "must test when new is called" do
xit "must test when new is called", pending: "Mock redesign" do
expect(test_class).to receive(:new).with(no_args).and_return(test_instance)
expect(test_instance).to receive(:test)
expect(test_class.new).to be(test_instance)

View file

@ -10,13 +10,13 @@ Spectator.describe "GitHub Issue #33" do
end
end
mock Test do
stub method2
end
# mock Test do
# stub method2
# end
describe Test do
describe "#method1" do
it do
xit pending: "Mock redesign" do
expect(subject).to receive(:method2)
subject.method1

View file

@ -25,12 +25,12 @@ class Sdk < SdkInterface
end
Spectator.describe Example do
mock Sdk do
stub register_hook(name, &block)
end
# mock Sdk do
# stub register_hook(name, &block)
# end
describe "#configure" do
it "registers a block on configure" do
xit "registers a block on configure", pending: "Mock redesign" do
sdk = Sdk.new
example_class = Example.new(sdk)
allow(sdk).to receive(register_hook())

View file

@ -5,7 +5,7 @@ Spectator.describe "eq matcher" do
expect(42).to eq(42)
end
it "is false for inequal values" do
it "is false for unequal values" do
expect(42).to_not eq(24)
end
@ -21,10 +21,12 @@ Spectator.describe "eq matcher" do
end
double(:fake) do
stub instance.==(other) { true }
stub def ==(other)
true
end
end
it "uses the == operator" do
xit "uses the == operator", pending: "Mock redesign" do
dbl = double(:fake)
expect(42).to eq(dbl)
expect(dbl).to have_received(:==).with(42).once