shard-spectator/spec/docs/mocks/overview_spec.cr
2020-03-28 10:50:58 -06:00

40 lines
757 B
Crystal

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