|
| 1 | +# Mocking the parser |
| 2 | + |
| 3 | +This example shows you how to use the provided [`fake` package](https://godoc.org/github.com/markus-wa/demoinfocs-golang/fake) to mock `demoinfocs.IParser` and other parts of the library. |
| 4 | +That way you will be able to write useful unit tests for your application. |
| 5 | + |
| 6 | +## System under test |
| 7 | + |
| 8 | +First, let's have a look at the API of our code, the 'system under test': |
| 9 | + |
| 10 | +```go |
| 11 | +import ( |
| 12 | + dem "github.com/markus-wa/demoinfocs-golang" |
| 13 | + events "github.com/markus-wa/demoinfocs-golang/events" |
| 14 | +) |
| 15 | + |
| 16 | +func collectKills(parser dem.IParser) (kills []events.Kill, err error) { |
| 17 | + ... |
| 18 | +} |
| 19 | +``` |
| 20 | + |
| 21 | +We deliberately ignore the implementation so we don't make assumptions about the code since it might change in the future. |
| 22 | + |
| 23 | +As you can see `collectKills` takes an `IParser` as input and returns a slice of `events.Kill` and potentially an error. |
| 24 | + |
| 25 | +## Positive test case |
| 26 | + |
| 27 | +Now let's have a look at our first test. Here we want to ensure that all kills are collected and that the order of the collected events is correct. |
| 28 | + |
| 29 | +```go |
| 30 | +import ( |
| 31 | + "errors" |
| 32 | + "testing" |
| 33 | + |
| 34 | + assert "github.com/stretchr/testify/assert" |
| 35 | + |
| 36 | + common "github.com/markus-wa/demoinfocs-golang/common" |
| 37 | + events "github.com/markus-wa/demoinfocs-golang/events" |
| 38 | + fake "github.com/markus-wa/demoinfocs-golang/fake" |
| 39 | +) |
| 40 | + |
| 41 | +func TestCollectKills(t *testing.T) { |
| 42 | + parser := fake.NewParser() |
| 43 | + kill1 := kill(common.EqAK47) |
| 44 | + kill2 := kill(common.EqScout) |
| 45 | + kill3 := kill(common.EqAUG) |
| 46 | + parser.MockEvents(kill1) // First frame |
| 47 | + parser.MockEvents(kill2, kill3) // Second frame |
| 48 | + |
| 49 | + parser.On("ParseToEnd").Return(nil) // Return no error |
| 50 | + |
| 51 | + actual, err := collectKills(parser) |
| 52 | + |
| 53 | + assert.Nil(t, err) |
| 54 | + expected := []events.Kill{kill1, kill2, kill3} |
| 55 | + assert.Equal(t, expected, actual) |
| 56 | +} |
| 57 | + |
| 58 | +func kill(wep common.EquipmentElement) events.Kill { |
| 59 | + eq := common.NewEquipment(wep) |
| 60 | + return events.Kill{ |
| 61 | + Killer: new(common.Player), |
| 62 | + Weapon: &eq, |
| 63 | + Victim: new(common.Player), |
| 64 | + } |
| 65 | +} |
| 66 | +``` |
| 67 | + |
| 68 | +As you can see we first create a mocked parser with `fake.NewParser()`. |
| 69 | + |
| 70 | +Then we create two `Kill` events and add them into the `Parser.Events` map. |
| 71 | +The map index indicates at which frame the events will be sent out, in our case that's during the first and second frame, as we just iterate over the slice indices. |
| 72 | + |
| 73 | +Note: Especially when used together with `Parser.NetMessages` it can be useful to set these indices manually to ensure the events and net-messages are sent at the right moment. |
| 74 | + |
| 75 | +## Negative test case |
| 76 | + |
| 77 | +Last but not least we want to do another test that ensures any error the parser encounters is returned to the callee and not suppressed by our function. |
| 78 | + |
| 79 | +```go |
| 80 | +import ( |
| 81 | + "errors" |
| 82 | + "testing" |
| 83 | + |
| 84 | + assert "github.com/stretchr/testify/assert" |
| 85 | + |
| 86 | + common "github.com/markus-wa/demoinfocs-golang/common" |
| 87 | + events "github.com/markus-wa/demoinfocs-golang/events" |
| 88 | + fake "github.com/markus-wa/demoinfocs-golang/fake" |
| 89 | +) |
| 90 | + |
| 91 | +func TestCollectKillsError(t *testing.T) { |
| 92 | + parser := fake.NewParser() |
| 93 | + expectedErr := errors.New("Test error") |
| 94 | + parser.On("ParseToEnd").Return(expectedErr) |
| 95 | + |
| 96 | + kills, actualErr := collectKills(parser) |
| 97 | + |
| 98 | + assert.Equal(t, expectedErr, actualErr) |
| 99 | + assert.Nil(t, kills) |
| 100 | +} |
| 101 | +``` |
| 102 | + |
| 103 | +This test simply tells the mock to return the specified error and asserts that our function returns it to us. |
| 104 | +It also makes sure that kills is nil, and not an empty slice. |
0 commit comments