Skip to content

Commit 48df40b

Browse files
authored
Merge pull request #107 from zcolleen/master
Updated readme with linters and Optional method + removed FailNow from MinimockFinish + fixed tests
2 parents f044740 + 2571fb1 commit 48df40b

25 files changed

+587
-400
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ all: install test lint
77
generate:
88
go run ./cmd/minimock/minimock.go -i github.com/gojuno/minimock/v3.Tester -o ./tests
99
go run ./cmd/minimock/minimock.go -i ./tests.Formatter -o ./tests/formatter_mock.go
10+
go run ./cmd/minimock/minimock.go -i ./tests.Formatter -o ./tests/formatter_with_custom_name_mock.go -n CustomFormatterNameMock
1011
go run ./cmd/minimock/minimock.go -i ./tests.genericInout -o ./tests/generic_inout.go
1112
go run ./cmd/minimock/minimock.go -i ./tests.genericOut -o ./tests/generic_out.go
1213
go run ./cmd/minimock/minimock.go -i ./tests.genericIn -o ./tests/generic_in.go

README.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ The main features of minimock are:
1616
* It supports generics.
1717
* 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.
1818
* It can generate several mocks in one run.
19-
* It generates code that passes [gometalinter](https://github.com/alecthomas/gometalinter) checks.
19+
* It generates code that passes default set of [golangci-lint](https://github.com/golangci/golangci-lint) checks.
2020
* 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.
2121
* It makes sure that all mocked methods have been called during the test and keeps your test code clean and up to date.
2222
* It provides When and Then helpers to set up several expectations and results for any method.
@@ -179,6 +179,16 @@ mc := minimock.NewController(t)
179179
formatterMock := NewFormatterMock(mc).FormatMock.Times(10).Expect("hello %s!", "world").Return("hello world!")
180180
```
181181

182+
There are also cases, when you don't know for sure if the mocking method would be called or not.
183+
But you still want to mock it, if it will be called. This is where "Optional" option comes into play:
184+
185+
```go
186+
mc := minimock.NewController(t)
187+
formatterMock := NewFormatterMock(mc).FormatMock.Optional().Expect("hello %s!", "world").Return("hello world!")
188+
```
189+
190+
When this option is set, it disables checking the call of mocking method.
191+
182192
### Mocking context
183193
Sometimes context gets modified by the time the mocked method is being called.
184194
However, in most cases you don't really care about the exact value of the context argument.

mock_controller.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ type Tester interface {
1717
Fatalf(format string, args ...interface{})
1818
Error(...interface{})
1919
Errorf(format string, args ...interface{})
20-
FailNow()
2120
Cleanup(f func())
2221
}
2322

mock_controller_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ type unsafeMocker struct {
7373
}
7474

7575
func (um *unsafeMocker) MinimockWait(time.Duration) {
76-
um.tester.FailNow()
76+
um.tester.Fatal()
7777
}
7878

7979
type unsafeTester struct {
@@ -82,6 +82,6 @@ type unsafeTester struct {
8282
finished bool
8383
}
8484

85-
func (u *unsafeTester) FailNow() {
85+
func (u *unsafeTester) Fatal(...interface{}) {
8686
u.finished = true
8787
}

safe_tester.go

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,3 @@ func (st *safeTester) Fatalf(format string, args ...interface{}) {
4242

4343
st.Tester.Fatalf(format, args...)
4444
}
45-
46-
// FailNow implements Tester
47-
func (st *safeTester) FailNow() {
48-
st.m.Lock()
49-
defer st.m.Unlock()
50-
51-
st.Tester.FailNow()
52-
}

template.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ const (
104104
// Marks this method to be optional. The default behavior of any method with Return() is '1 or more', meaning
105105
// the test will fail minimock's automatic final call check if the mocked method was not called at least once.
106106
// Optional() makes method check to work in '0 or more' mode.
107-
// It is NOT RECOMMENDED to use this option by default unless you really need it, as it helps to
107+
// It is NOT RECOMMENDED to use this option unless you really need it, as default behaviour helps to
108108
// catch the problems when the expected method call is totally skipped during test run.
109109
func ({{$m}} *m{{$mock}}{{$method.Name}}{{(paramsRef)}}) Optional() *m{{$mock}}{{$method.Name}}{{(paramsRef)}} {
110110
{{$m}}.optional = true
@@ -386,7 +386,6 @@ const (
386386
{{- range $method := $.Interface.Methods }}
387387
m.Minimock{{$method.Name}}Inspect()
388388
{{ end -}}
389-
m.t.FailNow()
390389
}
391390
})
392391
}

tests/actor_mock.go

Lines changed: 16 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/actor_mock_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,3 +83,12 @@ func TestActorMock_FailedToUseExpectParamsAfterSet(t *testing.T) {
8383
return
8484
}).ActionMock.ExpectFirstParamParam1("abc").Return(1, nil)
8585
}
86+
87+
func TestActorMock_Optional(t *testing.T) {
88+
tester := NewTesterMock(t)
89+
tester.CleanupMock.Return()
90+
91+
mock := NewActorMock(tester).ActionMock.Optional().Return(1, nil)
92+
93+
mock.MinimockFinish()
94+
}

tests/context_accepter_mock.go

Lines changed: 48 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/context_accepter_mock_test.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,7 @@ func TestContextAccepterMock_TimesFailure(t *testing.T) {
139139
tester := NewTesterMock(t)
140140
tester.CleanupMock.Return().
141141
ErrorfMock.Expect("Expected %d calls to ContextAccepterMock.AcceptContextWithStructArgs but found %d calls", uint64(1), uint64(2)).
142-
Return().
143-
FailNowMock.Return()
142+
Return()
144143

145144
// Expected 1 calls to ContextAccepterMock.AcceptContextWithStructArgs but found 2 calls
146145
mock := NewContextAccepterMock(tester).
@@ -183,8 +182,7 @@ func TestContextAccepterMock_ExpectedCall(t *testing.T) {
183182
tester := NewTesterMock(t)
184183
tester.CleanupMock.Times(1).Return().
185184
ErrorMock.Expect("Expected call to ContextAccepterMock.AcceptContext").Times(1).
186-
Return().
187-
FailNowMock.Times(1).Return()
185+
Return()
188186

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

0 commit comments

Comments
 (0)