Add spec for doubles docs

This commit is contained in:
Michael Miller 2022-07-14 13:18:13 -06:00
parent 1d58824e6c
commit 7dd77a2096
No known key found for this signature in database
GPG Key ID: 32B47AE8F388A1FF
1 changed files with 79 additions and 0 deletions

79
spec/docs/doubles_spec.cr Normal file
View File

@ -0,0 +1,79 @@
require "../spec_helper"
# https://gitlab.com/arctic-fox/spectator/-/wikis/Doubles
Spectator.describe "Doubles Docs" do
let(answer) { 42 }
double :my_double, answer: 5 do
def answer(arg1, arg2)
arg1 + arg2
end
end
it "does something" do
dbl = double(:my_double, answer: answer)
expect(dbl.answer).to eq(42)
expect(dbl.answer(1, 2)).to eq(3)
end
class Emitter
def initialize(@value : Int32)
end
def emit(target)
target.call(@value)
end
end
context "Expecting Behavior" do
describe Emitter do
subject { Emitter.new(42) }
double :target, call: nil
describe "#emit" do
it "invokes #call on the target" do
target = double(:target)
subject.emit(target)
expect(target).to have_received(:call).with(42)
end
end
end
it "does something" do
dbl = double(:my_double)
allow(dbl).to receive(:answer).and_return(42) # Merge this line...
dbl.answer
expect(dbl).to have_received(:answer) # and this line.
end
it "does something" do
dbl = double(:my_double)
expect(dbl).to receive(:answer).and_return(42)
dbl.answer
end
end
context "Class Doubles" do
double :my_double do
# Define class methods with `self.` prefix.
stub def self.something
42
end
end
it "does something" do
# Default stubs can be defined with key-value pairs (keyword arguments).
dbl = class_double(:my_double, something: 3)
expect(dbl.something).to eq(3)
# Stubs can be changed with `allow`.
allow(dbl).to receive(:something).and_return(5)
expect(dbl.something).to eq(5)
# Even the expect-receive syntax works.
expect(dbl).to receive(:something).and_return(7)
dbl.something
end
end
end