Actions
test.NewActionRequest allows to invoke an Action directly from a test.
GOX builds the request used by the Action protocol, executes the corresponding function and validates its error or result. Actions to know its operation within an application.
Request
| Property | Description |
|---|---|
URL | Route of the file that declares the Action. |
Method | Name of the Action. |
Params | Parameters sent to the function. |
request := test.NewActionRequest{
URL: "/users",
Method: "Create",
Params: test.ActionParams{
"name": "George",
},
} Method represents the name of the Action, not an HTTP method. Internally, the runner makes the invocation with the protocol used by the client.
Expected response
test.NewResponseActionExpected allows to validate two properties:
| Property | Description |
|---|---|
| Error | Error returned by the Action. |
| Result | Result returned by the Action. |
Error
test.ExpectedActionError.Nil() requires that the Action do not return a mistake. Exact(value) requires a specific error value.
expected := test.NewResponseActionExpected{
Error: test.ExpectedActionError.Nil(),
} expected := test.NewResponseActionExpected{
Error: test.ExpectedActionError.Exact("user not found"),
} Result
test.ExpectedActionResult.Exact() compare the full result. Partial() Valid only the included properties.
expected := test.NewResponseActionExpected{
Error: test.ExpectedActionError.Nil(),
Result: test.ExpectedActionResult.Partial(map[string]any{
"name": "George",
"created": true,
}),
} Partial() is applied to structured results and allows the Action to return additional properties.
Execution
The request and its expectation are added to a pipeline as any HTTP request:
result, match := test.Execute(test.Config{}, []test.ExecutePipeLine{
{Request: request, Expected: expected},
}) Cookies and session are preserved when an Action shares the pipeline with other operations.
Integration with Go
The parameters are represented by a map of values and converted to the format expected by the compiled Wrapper of the Action. The function receives the same types and passes the same validations as an invocation from the client.
The structured result is decoded before applying Exact() or Partial()A response that does not contain JSON retains its textual representation for comparison.
Best practices
- Use the exact name of the Action in
Method. - It is explicitly worth the fact that the error is nil in successful cases.
- Use
Partial()where only some properties are part of the contract. - Try the expected errors in addition to the successful flow.
- Combine Actions and HTTP routes only when they belong to the same flow.