-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6d3fc3f
commit dff5bfc
Showing
14 changed files
with
1,495 additions
and
102 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
name: docs | ||
on: | ||
push: | ||
tags: | ||
- 'v*.*.*' | ||
permissions: | ||
contents: write | ||
jobs: | ||
deploy: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v3 | ||
- uses: actions/setup-python@v4 | ||
with: | ||
python-version: 3.x | ||
- uses: actions/cache@v2 | ||
with: | ||
key: ${{ github.ref }} | ||
path: .cache | ||
- run: sudo apt-get install -y libcairo2-dev libfreetype6-dev libffi-dev libjpeg-dev libpng-dev libz-dev | ||
- run: pip install -r docs/requirements.txt | ||
env: | ||
GH_TOKEN: ${{ secrets.GH_TOKEN }} | ||
- name: Setup git | ||
run: | | ||
git config --global user.name mockio-actions | ||
git config --global user.email [email protected] | ||
git fetch origin gh-pages --depth=1 | ||
- name: Deploy docs | ||
run: "mike deploy --push --update-aliases $(echo ${{ github.ref_name }} | cut -d'.' -f1-2 | xargs printf '%s.0' ) latest" |
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 |
---|---|---|
@@ -1,2 +1,4 @@ | ||
coverage.txt | ||
.cache | ||
.cache | ||
.idea | ||
*.dylib |
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,68 @@ | ||
# Argument Captors | ||
|
||
Argument captors are a powerful feature that allow you to capture the arguments passed to a method when it is | ||
called. This is useful when you want to verify that a method was called with specific arguments, but you don't know what | ||
those arguments will be ahead of time. | ||
|
||
## Creating a Captor | ||
|
||
To create a captor, you simply call the `Captor` function with the type of the argument you want to capture: | ||
|
||
```go | ||
c := Captor[string]() | ||
``` | ||
|
||
## Using a Captor | ||
|
||
To use a captor, you pass it as an argument to the `When` function. When the method is called, the captor will capture the | ||
argument and store it in the captor's value: | ||
|
||
```go | ||
When(greeter.Greet(c.Capture())).ThenReturn("Hello, world!") | ||
``` | ||
|
||
## Retrieving the Captured Values | ||
|
||
Argument captor records an argument on each stub call. You can retrieve the captured values by calling the `Values` method | ||
|
||
```go | ||
capturedValues := c.Values() | ||
``` | ||
|
||
If you want to retrieve just the last captured value, you can call the `Last` method | ||
|
||
```go | ||
capturedValue := c.Last() | ||
``` | ||
|
||
## Example usage | ||
|
||
In this example we will create a mock, and use an argument captor to capture the arguments passed to the `Greet` method: | ||
|
||
```go | ||
package main | ||
|
||
import ( | ||
. "github.com/ovechkin-dm/mockio/mock" | ||
"testing" | ||
) | ||
|
||
type Greeter interface { | ||
Greet(name any) string | ||
} | ||
|
||
func TestSimple(t *testing.T) { | ||
SetUp(t) | ||
greeter := Mock[Greeter]() | ||
c := Captor[string]() | ||
When(greeter.Greet(c.Capture())).ThenReturn("Hello, world!") | ||
_ = greeter.Greet("John") | ||
_ = greeter.Greet("Jane") | ||
if c.Values()[0] != "John" { | ||
t.Error("Expected John, got", c.Values()[0]) | ||
} | ||
if c.Values()[1] != "Jane" { | ||
t.Error("Expected Jane, got", c.Values()[1]) | ||
} | ||
} | ||
``` |
Oops, something went wrong.