Skip to content

Commit

Permalink
Merge pull request #107 from zcolleen/master
Browse files Browse the repository at this point in the history
Updated readme with linters and Optional method + removed FailNow from MinimockFinish + fixed tests
  • Loading branch information
zcolleen authored Jun 7, 2024
2 parents f044740 + 2571fb1 commit 48df40b
Show file tree
Hide file tree
Showing 25 changed files with 587 additions and 400 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ all: install test lint
generate:
go run ./cmd/minimock/minimock.go -i github.com/gojuno/minimock/v3.Tester -o ./tests
go run ./cmd/minimock/minimock.go -i ./tests.Formatter -o ./tests/formatter_mock.go
go run ./cmd/minimock/minimock.go -i ./tests.Formatter -o ./tests/formatter_with_custom_name_mock.go -n CustomFormatterNameMock
go run ./cmd/minimock/minimock.go -i ./tests.genericInout -o ./tests/generic_inout.go
go run ./cmd/minimock/minimock.go -i ./tests.genericOut -o ./tests/generic_out.go
go run ./cmd/minimock/minimock.go -i ./tests.genericIn -o ./tests/generic_in.go
Expand Down
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ The main features of minimock are:
* It supports generics.
* It works well with [table driven tests](https://dave.cheney.net/2013/06/09/writing-table-driven-tests-in-go) because you can set up mocks for several methods in one line of code using the builder pattern.
* It can generate several mocks in one run.
* It generates code that passes [gometalinter](https://github.com/alecthomas/gometalinter) checks.
* It generates code that passes default set of [golangci-lint](https://github.com/golangci/golangci-lint) checks.
* It puts //go:generate instruction into the generated code, so all you need to do when the source interface is updated is to run the `go generate ./...` command from within the project's directory.
* It makes sure that all mocked methods have been called during the test and keeps your test code clean and up to date.
* It provides When and Then helpers to set up several expectations and results for any method.
Expand Down Expand Up @@ -179,6 +179,16 @@ mc := minimock.NewController(t)
formatterMock := NewFormatterMock(mc).FormatMock.Times(10).Expect("hello %s!", "world").Return("hello world!")
```

There are also cases, when you don't know for sure if the mocking method would be called or not.
But you still want to mock it, if it will be called. This is where "Optional" option comes into play:

```go
mc := minimock.NewController(t)
formatterMock := NewFormatterMock(mc).FormatMock.Optional().Expect("hello %s!", "world").Return("hello world!")
```

When this option is set, it disables checking the call of mocking method.

### Mocking context
Sometimes context gets modified by the time the mocked method is being called.
However, in most cases you don't really care about the exact value of the context argument.
Expand Down
1 change: 0 additions & 1 deletion mock_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ type Tester interface {
Fatalf(format string, args ...interface{})
Error(...interface{})
Errorf(format string, args ...interface{})
FailNow()
Cleanup(f func())
}

Expand Down
4 changes: 2 additions & 2 deletions mock_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ type unsafeMocker struct {
}

func (um *unsafeMocker) MinimockWait(time.Duration) {
um.tester.FailNow()
um.tester.Fatal()
}

type unsafeTester struct {
Expand All @@ -82,6 +82,6 @@ type unsafeTester struct {
finished bool
}

func (u *unsafeTester) FailNow() {
func (u *unsafeTester) Fatal(...interface{}) {
u.finished = true
}
8 changes: 0 additions & 8 deletions safe_tester.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,3 @@ func (st *safeTester) Fatalf(format string, args ...interface{}) {

st.Tester.Fatalf(format, args...)
}

// FailNow implements Tester
func (st *safeTester) FailNow() {
st.m.Lock()
defer st.m.Unlock()

st.Tester.FailNow()
}
3 changes: 1 addition & 2 deletions template.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ const (
// Marks this method to be optional. The default behavior of any method with Return() is '1 or more', meaning
// the test will fail minimock's automatic final call check if the mocked method was not called at least once.
// Optional() makes method check to work in '0 or more' mode.
// It is NOT RECOMMENDED to use this option by default unless you really need it, as it helps to
// It is NOT RECOMMENDED to use this option unless you really need it, as default behaviour helps to
// catch the problems when the expected method call is totally skipped during test run.
func ({{$m}} *m{{$mock}}{{$method.Name}}{{(paramsRef)}}) Optional() *m{{$mock}}{{$method.Name}}{{(paramsRef)}} {
{{$m}}.optional = true
Expand Down Expand Up @@ -386,7 +386,6 @@ const (
{{- range $method := $.Interface.Methods }}
m.Minimock{{$method.Name}}Inspect()
{{ end -}}
m.t.FailNow()
}
})
}
Expand Down
17 changes: 16 additions & 1 deletion tests/actor_mock.go

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

9 changes: 9 additions & 0 deletions tests/actor_mock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,12 @@ func TestActorMock_FailedToUseExpectParamsAfterSet(t *testing.T) {
return
}).ActionMock.ExpectFirstParamParam1("abc").Return(1, nil)
}

func TestActorMock_Optional(t *testing.T) {
tester := NewTesterMock(t)
tester.CleanupMock.Return()

mock := NewActorMock(tester).ActionMock.Optional().Return(1, nil)

mock.MinimockFinish()
}
49 changes: 48 additions & 1 deletion tests/context_accepter_mock.go

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

6 changes: 2 additions & 4 deletions tests/context_accepter_mock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,7 @@ func TestContextAccepterMock_TimesFailure(t *testing.T) {
tester := NewTesterMock(t)
tester.CleanupMock.Return().
ErrorfMock.Expect("Expected %d calls to ContextAccepterMock.AcceptContextWithStructArgs but found %d calls", uint64(1), uint64(2)).
Return().
FailNowMock.Return()
Return()

// Expected 1 calls to ContextAccepterMock.AcceptContextWithStructArgs but found 2 calls
mock := NewContextAccepterMock(tester).
Expand Down Expand Up @@ -183,8 +182,7 @@ func TestContextAccepterMock_ExpectedCall(t *testing.T) {
tester := NewTesterMock(t)
tester.CleanupMock.Times(1).Return().
ErrorMock.Expect("Expected call to ContextAccepterMock.AcceptContext").Times(1).
Return().
FailNowMock.Times(1).Return()
Return()

mock := NewContextAccepterMock(tester).AcceptContextMock.Return()

Expand Down
17 changes: 16 additions & 1 deletion tests/formatter_mock.go

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

5 changes: 0 additions & 5 deletions tests/formatter_mock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ func TestFormatterMock_CleanupIsCalled(t *testing.T) {
tester := NewTesterMock(t)
tester.CleanupMock.Set(t.Cleanup)
tester.ErrorMock.Expect("Expected call to FormatterMock.Format").Return()
tester.FailNowMock.Return()

NewFormatterMock(tester).FormatMock.Return("")
}
Expand Down Expand Up @@ -154,7 +153,6 @@ func TestFormatterMock_ReturnWithoutExpectForFixedArgsMethod(t *testing.T) {
tester := NewTesterMock(t).CleanupMock.Return()

tester.ErrorMock.Expect("Expected call to FormatterMock.Format")
tester.FailNowMock.Expect()

formatterMock := NewFormatterMock(tester)
formatterMock.FormatMock.Return("")
Expand Down Expand Up @@ -232,7 +230,6 @@ func TestFormatterMock_MinimockFinish(t *testing.T) {
tester := NewTesterMock(t).CleanupMock.Return()

tester.ErrorMock.Expect("Expected call to FormatterMock.Format").Return()
tester.FailNowMock.Expect().Return()

formatterMock := NewFormatterMock(tester)
formatterMock.FormatMock.Set(func(string, ...interface{}) string { return "" })
Expand All @@ -246,7 +243,6 @@ func TestFormatterMock_MinimockFinish_WithNoMetExpectations(t *testing.T) {
tester.ErrorfMock.Set(func(m string, args ...interface{}) {
assert.Equal(t, m, "Expected call to FormatterMock.Format with params: %#v")
})
tester.FailNowMock.Expect().Return()

formatterMock := NewFormatterMock(tester)
formatterMock.FormatMock.Expect("a").Return("a")
Expand All @@ -259,7 +255,6 @@ func TestFormatterMock_MinimockWait(t *testing.T) {
tester := NewTesterMock(t).CleanupMock.Return()

tester.ErrorMock.Expect("Expected call to FormatterMock.Format").Return()
tester.FailNowMock.Expect().Return()

formatterMock := NewFormatterMock(tester)
formatterMock.FormatMock.Set(func(string, ...interface{}) string { return "" })
Expand Down
Loading

0 comments on commit 48df40b

Please sign in to comment.