Skip to content

Commit

Permalink
README
Browse files Browse the repository at this point in the history
  • Loading branch information
hervit0 committed Mar 3, 2023
1 parent bd38832 commit b4c95c6
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 1 deletion.
10 changes: 9 additions & 1 deletion .idea/momock.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,20 @@ Generating Go mocks for testing.

### Features / Roadmap

| Feature | Status |
|-----------------------------------------------------|--------------------|
| Creating mock file | :white_check_mark: |
| Choosing mock file location | :x: |
| Choosing mock file package name | :x: |
| Choosing mock struct name | :x: |
| Assigning build tag | :soon: |
| Choosing assertion framework | :x: |
| Matching statically typed arguments | :white_check_mark: |
| Returning statically typed outputs | :white_check_mark: |
| Matching multiple invocations | :white_check_mark: |
| Erroring if expectations were set but not triggered | :soon: |
| Erroring if expectations were not set but triggered | :white_check_mark: |

## Usage

Place that generate directive at the top of file containing the interface you want to generate a mock for:
Expand All @@ -27,6 +41,7 @@ Use native `generate` go command:
```shell
go generate ./...
```
It will generate a mock file (`*_mock.go`) alongside your interface file. Please do not modify it.

Let's say you have a service using the previous interface:
```go
Expand Down Expand Up @@ -89,6 +104,12 @@ Not matching the arguments, will generate that error:
Wanted: hello
```

Providing the wrong number of arguments, will generate that error:
```go
=== RUN Test_WrongNumberOfArguments
manager.go:57: Wrong number of inputs
```

## Installation

At the root of the project, extend your `tools.go` file:
Expand Down
2 changes: 2 additions & 0 deletions example/consumer.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build example

package playground

import "context"
Expand Down
59 changes: 59 additions & 0 deletions example/consumer_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build example

package playground_test

import (
Expand Down Expand Up @@ -59,6 +61,32 @@ func Test_WrongArgumentsToTheMock(t *testing.T) {
}
}

func Test_WrongNumberOfArguments(t *testing.T) {
// given
ctx := context.TODO()

mock := playground.NewMockSomeInterface(t)
mock.MockManager.SetExpectations([]momock.MockCall{
{
Method: mock.Do,
In: []any{ctx, "hello", "EXTRA HELLO"},
Out: []any{nil, nil},
},
})

myStruct := playground.MyService{
TheInterface: mock,
}

// when
result := myStruct.UseInterface(ctx)

// then
if result != nil {
t.Fatalf("was expecting nil")
}
}

func Test_ForgotToSetExpectations(t *testing.T) {
// given
ctx := context.TODO()
Expand All @@ -77,3 +105,34 @@ func Test_ForgotToSetExpectations(t *testing.T) {
t.Fatalf("was expecting nil")
}
}

func Test_ExpectationsWereNotMet(t *testing.T) {
// given
ctx := context.TODO()

mock := playground.NewMockSomeInterface(t)
mock.MockManager.SetExpectations([]momock.MockCall{
{
Method: mock.Do,
In: []any{ctx, "hello"},
Out: []any{nil, nil},
},
{
Method: mock.Do,
In: []any{ctx, "GOODNIGHT"},
Out: []any{nil, nil},
},
})

myStruct := playground.MyService{
TheInterface: mock,
}

// when
result := myStruct.UseInterface(ctx)

// then
if result != nil {
t.Fatalf("was expecting nil")
}
}
2 changes: 2 additions & 0 deletions example/playground.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build example

//go:generate go run $GOPATH/src/momock/main.go

package playground
Expand Down
2 changes: 2 additions & 0 deletions example/playground_mock.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build example

package playground

import (
Expand Down

0 comments on commit b4c95c6

Please sign in to comment.