steinfletcher / apitest

A simple and extensible behavioural testing library for Go. You can use api test to simplify REST API, HTTP handler and e2e tests.
https://apitest.dev
MIT License
778 stars 55 forks source link

feat(mocks): added support for reusing mocks any times #154

Closed nomadesoft closed 2 weeks ago

nomadesoft commented 2 weeks ago

Summary

The PR adds support for reusing mocks any number of times by introducing a new AnyTimes method. This method allows a mock to be invoked multiple times without failing the test if it is not used the expected number of times.

Motivation

This feature is very useful in scenarios where the exact number of invocations is not known or not important.

Changes

  1. apitest.go:

    • Modified the assertMocks method to check for anyTimesSet before failing the test if the mock was not invoked the expected number of times.
    • Updated the Mocks method to handle mocks with anyTimesSet.
  2. apitest_test.go:

    • Added a new test TestApiTest_MatchesAnyTimes to verify the functionality of the AnyTimes method.
  3. mocks.go:

    • Added a new boolean field anyTimesSet to the Mock struct.
    • Modified the matches function to skip used mocks if anyTimesSet is true.
    • Added a new method AnyTimes to the MockResponse struct to set the anyTimesSet field.
  4. README.md:

    • Updated the documentation to include usage instructions for the new AnyTimes method.