Skip to content

Commit

Permalink
working implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
hackeryarn committed May 20, 2018
1 parent 14ef383 commit b59e71f
Showing 1 changed file with 35 additions and 1 deletion.
36 changes: 35 additions & 1 deletion mocking.md
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,7 @@ type ConfigurableSleeper struct {
}
```

We are using `duration` to configure the time slept and `sleep` as a way to pass in a custom sleep function, including a spy.
We are using `duration` to configure the time slept and `sleep` as a way to pass in a sleep function. The signature of `sleep` is the same as for `time.Sleep` allowing us to use `time.Sleep` in our real implementation and a spy in our tests.

```go
type SpyTime struct {
Expand Down Expand Up @@ -522,6 +522,40 @@ func TestConfigurableSleeper(t *testing.T) {
}
```

There should be nothing new in this test and it is setup very similar to the previous mock tests.

### Try and run the test
```
sleeper.Sleep undefined (type ConfigurableSleeper has no field or method Sleep, but does have sleep)
```

You should see a very clear error message indicating that we do not have a `Sleep` method created on our `ConfigurableSleeper`.

### Write the minimal amount of code for the test to run and check failing test output
```go
func (c *ConfigurableSleeper) Sleep() {
}
```

With our new `Sleep` function implemented we have a failing test.

```
countdown_test.go:56: should have slept for 5s but slept for 0s
```

### Write enough code to make it pass

All we need to do not is implement the `Sleep` function for `ConfigurableSleeper`.

```go
func (c *ConfigurableSleeper) Sleep() {
c.sleep(c.duration)
}
```

With this change all of the test should be passing again.

## But isn't mocking evil?

You may have heard mocking is evil. Just like anything in software development it can be used for evil, just like [DRY](https://en.wikipedia.org/wiki/Don%27t_repeat_yourself).
Expand Down

0 comments on commit b59e71f

Please sign in to comment.