-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add the Context option to the mock definition #minor (#11)
- Loading branch information
Showing
5 changed files
with
137 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package mock | ||
|
||
type ContextOption func(context *Context) *Context | ||
|
||
func WithCallLimit(times int) ContextOption { | ||
return func(context *Context) *Context { | ||
if context == nil { | ||
context = &Context{} | ||
} | ||
context.Times = times | ||
return context | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package mock_test | ||
|
||
import ( | ||
"github.com/churmd/smockerclient/mock" | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
) | ||
|
||
func TestWithCallLimit(t *testing.T) { | ||
t.Run("it creates a new context with the times key if one doesnt exist", func(t *testing.T) { | ||
req := createRequest() | ||
res := createResponse() | ||
def := mock.NewDefinition(req, res, mock.WithCallLimit(3)) | ||
|
||
expectedDef := mock.Definition{ | ||
Request: req, | ||
Response: res, | ||
Context: &mock.Context{Times: 3}, | ||
} | ||
assert.Equal(t, expectedDef, def) | ||
}) | ||
|
||
t.Run("if a context already exists, it uses that", func(t *testing.T) { | ||
req := createRequest() | ||
res := createResponse() | ||
def := mock.NewDefinition(req, res, mock.WithCallLimit(3), mock.WithCallLimit(5)) | ||
|
||
expectedDef := mock.Definition{ | ||
Request: req, | ||
Response: res, | ||
Context: &mock.Context{Times: 5}, | ||
} | ||
assert.Equal(t, expectedDef, def) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters