Add example usage of double

This commit is contained in:
Michael Miller 2020-03-07 17:04:28 -07:00
parent 5ad1277bdd
commit ce4fefaab2

View file

@ -239,7 +239,7 @@ Spectator supports multiple options for running tests.
Tests can be filtered by their location and name. Tests can be filtered by their location and name.
Additionally, tests can be randomized. Additionally, tests can be randomized.
Spectator can be configured with command-line arguments, Spectator can be configured with command-line arguments,
a config block in a `spec_helper.cr` file, and `.spectator` config file. a configure block in a `spec_helper.cr` file, and `.spectator` configuration file.
```crystal ```crystal
Spectator.configure do |config| Spectator.configure do |config|
@ -249,6 +249,29 @@ Spectator.configure do |config|
end end
``` ```
### Mocks and Doubles
Spectator supports an extensive mocking feature set via two types - mocks and doubles.
Mocks are used to override behavior in existing types.
Doubles are objects that stand-in when there are no type restrictions.
Stubs can be defined on both that control how methods behave.
```crystal
double :my_double do
stub foo : Int32
stub bar(arg) { arg.to_s }
end
it "does a thing" do
dbl = double(:my_double)
allow(dbl).to receive(:foo).and_return(42)
expect(dbl.foo).to eq(42)
expect(dbl.bar(42)).to eq("42")
end
```
For details on mocks and doubles, see the [wiki](https://gitlab.com/arctic-fox/spectator/-/wikis/Mocks-and-Doubles).
### Output ### Output
Spectator matches Crystal's default Spec output with some minor changes. Spectator matches Crystal's default Spec output with some minor changes.