Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Conditionally enable/disable Unset call
Consider the following example test scenario: The goal is to test http server API. The test may set up a local http server, database and mocks to 3rd party services. Since the tests are calling mocked server it may be useful to use mock `EXPECT()` calls. Test structure: 1. Setup local server, database and mocks 2. Mock 3rd party service calls 3. Send request to local server 4. Assert response 5. Verify side effects - execute database query As an enhancement, such tests can easily be called against deployed application, by simply replacing server and database URLs. The only catch are mocked calls, which won't be executed, since the request is sent to a remote server. In such case it may be possible to call `Unset()` method with `WithUnsetToggle` option, which value is set based on the current environment. For example: ```go func Env() mock.UnsetOption { if os.Getenv("local") { return mock.WithUnsetToggle(false) } return mock.WithUnsetToggle(true) } // In test ... func TestAPI(t *testing.T) { // setup server, database and mocks mock.Call3rdParty(ctx, argument).Return(nil).Unset(Env()) response := client.Post(request) // assertions and database query } ``` In the above example, if environment is "local" Unset will be disabled, meaning that all mocks will execute. If environment is other than "local" the Unset will be enabled, meaning that mocks will not be called. Signed-off-by: Piotr Persona <[email protected]>
- Loading branch information