pax_global_header 0000666 0000000 0000000 00000000064 15130526717 0014521 g ustar 00root root 0000000 0000000 52 comment=ce958b4e6f305d6666a3c8025bb3dee20d6edd23 flexmock-0.13.0/ 0000775 0000000 0000000 00000000000 15130526717 0013412 5 ustar 00root root 0000000 0000000 flexmock-0.13.0/.fmf/ 0000775 0000000 0000000 00000000000 15130526717 0014240 5 ustar 00root root 0000000 0000000 flexmock-0.13.0/.fmf/version 0000664 0000000 0000000 00000000002 15130526717 0015640 0 ustar 00root root 0000000 0000000 1 flexmock-0.13.0/.github/ 0000775 0000000 0000000 00000000000 15130526717 0014752 5 ustar 00root root 0000000 0000000 flexmock-0.13.0/.github/workflows/ 0000775 0000000 0000000 00000000000 15130526717 0017007 5 ustar 00root root 0000000 0000000 flexmock-0.13.0/.github/workflows/ci.yml 0000664 0000000 0000000 00000004274 15130526717 0020134 0 ustar 00root root 0000000 0000000 name: Test and lint on: workflow_dispatch: push: branches: [master] pull_request: branches: - "**" env: PYTHON_VERSION: "3.10" jobs: test: runs-on: ubuntu-latest strategy: fail-fast: false matrix: python-version: [ "pypy-3.10", "pypy-3.11", "3.10", "3.11", "3.12", "3.13", "3.14", ] steps: - uses: actions/checkout@v6 - name: Set up Python ${{ matrix.python-version }} uses: actions/setup-python@v6 with: python-version: ${{ matrix.python-version }} cache: "pip" - name: Install needed dependencies run: | pip install tox tox-gh-actions - name: Run tests with tox run: tox lint: runs-on: ubuntu-latest env: TARGETS: src tests steps: - uses: actions/checkout@v6 - name: Install Poetry run: pipx install poetry - name: Set up Python uses: actions/setup-python@v6 with: python-version: ${{ env.PYTHON_VERSION }} cache: "poetry" - name: Install dependencies run: | poetry config virtualenvs.in-project true poetry install - name: Run isort run: poetry run isort --check-only ${TARGETS} - name: Run black run: poetry run black --check ${TARGETS} - name: Run mypy run: poetry run mypy ${TARGETS} - name: Run pylint run: poetry run pylint ${TARGETS} test-docs: runs-on: ubuntu-latest steps: - uses: actions/checkout@v6 - name: Install Poetry run: pipx install poetry - name: Set up Python uses: actions/setup-python@v6 with: python-version: ${{ env.PYTHON_VERSION }} cache: "poetry" - name: Install dependencies run: | poetry config virtualenvs.in-project true poetry install - name: Generate man pages with Sphinx run: poetry run python setup.py build_sphinx -W --keep-going -n -b man --build-dir build/sphinx - name: Build docs with Mkdocs run: poetry run make docs flexmock-0.13.0/.github/workflows/publish.yml 0000664 0000000 0000000 00000002324 15130526717 0021201 0 ustar 00root root 0000000 0000000 name: Publish the release to PyPI on: release: types: [created] permissions: id-token: write env: PYTHON_VERSION: "3.10" jobs: build: runs-on: ubuntu-latest steps: - name: Set release name as environment variable run: echo "RELEASE_NAME=${{ github.event.release.tag_name }}" >> $GITHUB_ENV - name: Checkout code uses: actions/checkout@v6 - name: Install Poetry run: pipx install poetry - name: Verify version matches tag run: | POETRY_VERSION=$(poetry version -s) RELEASE_TAG=${{ github.event.release.tag_name }} # Strip 'v' prefix if exists CLEAN_TAG=${RELEASE_TAG#v} if [ "$POETRY_VERSION" != "$CLEAN_TAG" ]; then echo "Version mismatch!" echo "Poetry version: $POETRY_VERSION" echo "Release tag: $RELEASE_TAG" exit 1 fi - name: Set up Python uses: actions/setup-python@v6 with: python-version: ${{ env.PYTHON_VERSION }} cache: "poetry" - name: Build the release package run: poetry build - name: Publish the release to PyPI uses: pypa/gh-action-pypi-publish@release/v1 flexmock-0.13.0/.gitignore 0000664 0000000 0000000 00000000211 15130526717 0015374 0 ustar 00root root 0000000 0000000 __pycache__/ *.py[cod] .coverage docs/_build build/ dist/ *.egg-info .venv .pytest_cache .mypy_cache poetry.toml .tox _trial_temp site flexmock-0.13.0/.packit.yml 0000664 0000000 0000000 00000000671 15130526717 0015472 0 ustar 00root root 0000000 0000000 actions: post-upstream-clone: - wget -O fedora/python-flexmock.spec https://src.fedoraproject.org/rpms/python-flexmock/raw/rawhide/f/python-flexmock.spec srpm_build_deps: - wget upstream_package_name: flexmock downstream_package_name: python-flexmock specfile_path: fedora/python-flexmock.spec jobs: - job: copr_build trigger: pull_request targets: - fedora-all - job: tests trigger: pull_request targets: - fedora-all flexmock-0.13.0/.readthedocs.yml 0000664 0000000 0000000 00000000450 15130526717 0016477 0 ustar 00root root 0000000 0000000 version: 2 mkdocs: configuration: mkdocs.yml build: os: ubuntu-24.04 tools: python: "3.10" jobs: post_create_environment: - pip install poetry post_install: - poetry config virtualenvs.create false - VIRTUAL_ENV=$READTHEDOCS_VIRTUALENV_PATH poetry install flexmock-0.13.0/CHANGELOG.md 0000664 0000000 0000000 00000011303 15130526717 0015221 0 ustar 00root root 0000000 0000000 # Changelog This project follows semantic versioning. Types of changes: - **Added**: New features. - **Changed**: Changes in existing functionality. - **Deprecated**: Soon-to-be removed features. - **Removed**: Removed features. - **Fixed**: Bug fixes. - **Infrastructure**: Changes in build or deployment infrastructure. - **Documentation**: Changes in documentation. ## Unreleased - ## Release 0.13.0 ### Added - Add Python 3.14 support. ### Removed - Drop Python 3.8 and Python 3.9 support. ### Infrastructure - Add Pytest 9.0 to tox test run. ## Release 0.12.2 ### Fixed - Fix typing of `and_raise` (exception instances are allowed). - Fix teardown for spying on derived class methods. ### Documentation - Remove python 3.6 mentions and add py-version config to pylint. ## Release 0.12.1 ### Fixed - Flexmock Pytest plugin entrypoint missing from `setup.py`. ## Release 0.12.0 ### Added - Add Python 3.12 and 3.13 support. ### Changed - Add tox.ini to sdist to make the downstream testing easier. ### Removed - Drop Python 3.6 and 3.7 support. - Drop Pytest 5.x support. ## Fixed - Fixed the pytest integration by switching from wrapping of the test runner to the pytest plugin system. ## Release 0.11.3 ### Added - Add PEP 561 `py.typed` marker file. ### Changed - Remove documentation and test files from wheels build. - Re-organize unit tests. ### Documentation - Add a warning about the usage of `.new_instances()` method in the documentation. ## Release 0.11.2 ### Fixed - Fix subunit testrunner integration is broken. - Fix: TeamCity (PyCharm) testrunner integration is broken. ### Infrastructure - Run tests with testtools, subunit, TeamCity, and doctest testrunners using tox. ### Documentation - Test flexmock API examples using doctest. - Re-add Sphinx support for generating man pages. - Fix 404 page not loading CSS and Javascript resources in documentation. - Small fixes to documentation. ## Release 0.11.1 ### Fixed - Fix Zope testrunner integration is broken. ### Infrastructure - Run tests with Zope testrunner using tox. ## Release 0.11.0 ### Added - Add Python 3.8, 3.9, 3.10, and 3.11 support. - Add type annotations. ### Changed - **BREAKING CHANGE**: Flexmock needs to be imported explicitly using `from flexmock import flexmock`. The hack that allowed flexmock to be imported directly using `import flexmock` did not work well with static analysis tools. - Many error messages have been improved. - Undocumented methods `Expectation.reset`, `Expectation.verify`, and `Expectation.match_args` that were unintentionally left public are now private methods. - Undocumented attributes in `Mock` and `Expectation` are now private. These attributes were never meant to be accessed directly. ### Removed - Drop Python 2.7, 3.4, 3.5 support. - Drop Pytest 4.x support. - Remove unittest2 and nose integrations. unittest2 and nose are not maintained anymore. - **BREAKING CHANGE**: Removed support for calling `once`, `twice`, `never`, and `mock` methods without parentheses. This allows code completion and static analysis to work with these methods. ### Fixed - Fix `should_call` is broken if called on a fake object. - Fix `and_raise` allows invalid arguments for an exception. ### Infrastructure - Run linters and tests using Github Actions. - Add coverage reporting using Codecov. ### Documentation - Add contribution documentation. - Use Mkdocs instead of Sphinx to build the documentation. ## Release 0.10.10 ### Fixed - Fix AttributeError raised when mocking a proxied object. ## Release 0.10.9 ### Fixed - Fix flexmock not mocking methods properly on derived classes. ## Release 0.10.8 ### Fixed - Fix `with_args` not working built-in functions. ## Release 0.10.7 ### Fixed - Fix `with_args` not working built-in functions and methods. - Fix previous pytest `--durations` fix not working. ## Release 0.10.6 ### Fixed - Fix flexmock broken with Pytest 4 & 5. - Fix new_instances method not working with Python 2.7. - Fix multiple expectations for the same classmethod are not matched. ## Release 0.10.5 ### Added - Improve error message on unmatched method signature expectation. ### Fixed - Fix using `should_call` passes wrong `runtime_self`. - Fix pytest `--durations` flag when flexmock is installed. ## Release 0.10.4 ### Added - Add Python 3.6 and 3.7 support. ### Removed - Drop Python 2.6, 3.3, and Jython support. ### Fixed - Don't hide exception when flexmock is used as context manager. - Fix expectation reset for static methods on PyPy 2. - Ensure original exception is not suppressed in pytest hook. Looking for older changelog entries? See [CHANGELOG](https://github.com/flexmock/flexmock/blob/884ed669e36140c514e362d2dee71433db1394f9/CHANGELOG) file in git history. flexmock-0.13.0/CONTRIBUTING.md 0000664 0000000 0000000 00000004762 15130526717 0015654 0 ustar 00root root 0000000 0000000 # Contributing ## Setting up the development environment The easiest way to setup your development environment and install the needed dependendecies is to use Poetry. For instructions, see [Poetry Docs - Installation](https://python-poetry.org/docs/#installation). After installing Poetry you can use the following command to install all the needed dependencies: ``` poetry install ``` After installing the dependencies, you can enter the Python virtual environment that Poetry created with the following command: ``` poetry shell ``` ## Running tests and linters Tests and code linters are executed in CI pipeline when you open a new pull request, but you can also execute them locally. This project contains a Makefile that allows you to easily run all tests and linters with one command: ```sh # Run tests and linters make # Run only tests make test # Run only linters make lint ``` ## Running tests with multiple Python versions using Tox CI pipeline also runs tests using all the supported Python versions. You can use Tox if you want to run these tests yourself, but first you need to have all different Python versions available in your local system. One option is to use [Pyenv](https://github.com/pyenv/pyenv) to manage different Python versions. For example, if you would want to run the test suite using Python versions 3.12 and 3.13, you can install the needed Python versions with the following commands: ``` pyenv install 3.12 pyenv install 3.13 ``` After the installation, you can make the installed Python versions available globally: ``` pyenv global 3.12 3.13 ``` Finally, you can run Tox and it should discover the installed Python version automatically: ``` tox -e py312,py313 ``` You can omit the `-e` argument if you want to run the tests against all supported Python versions. If everything works, the output should be something similar to this: ``` ... __________ summary __________ py312: commands succeeded py313: commands succeeded congratulations :) ``` ## Updating documentation The documentation is built using [Mkdocs](https://www.mkdocs.org/). After installing the development dependencies, you can simply run the following command to serve the documentation on your local machine: ``` mkdocs serve ``` For more details, see [Mkdocs - User guide](https://www.mkdocs.org/user-guide/) and [Mkdocs Material theme documentation](https://squidfunk.github.io/mkdocs-material/). The documentation is automatically deployed to Read the Docs service on every new commit to the default branch. flexmock-0.13.0/LICENSE 0000664 0000000 0000000 00000002776 15130526717 0014433 0 ustar 00root root 0000000 0000000 Copyright 2011-2015 Herman Sheremetyev, Slavek Kabrda. All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. The views and conclusions contained in the software and documentation are those of the authors and should not be interpreted as representing official policies, either expressed or implied, of the FreeBSD Project. flexmock-0.13.0/MANIFEST.in 0000664 0000000 0000000 00000000272 15130526717 0015151 0 ustar 00root root 0000000 0000000 include *.py include CHANGELOG.md include LICENSE include README.md include tox.ini recursive-include tests *py recursive-include docs *.md include docs/conf.py include docs/toctree.rst flexmock-0.13.0/Makefile 0000664 0000000 0000000 00000003352 15130526717 0015055 0 ustar 00root root 0000000 0000000 color := $(shell tput setaf 2) off := $(shell tput sgr0) PYTHON := $(if $(shell command -v python3),python3,python) TARGETS = src tests .PHONY: all all: lint test .PHONY: lint lint: isort black mypy pylint .PHONY: test test: install unittest doctest pytest .PHONY: install install: @printf '\n\n*****************\n' @printf '$(color)Install flexmock$(off)\n' @printf '*****************\n' ifeq (${VIRTUAL_ENV},) @printf 'Skipping install. VIRTUAL_ENV is not set.\n' else pip install . endif .PHONY: pytest pytest: @printf '\n\n*****************\n' @printf '$(color)Running pytest$(off)\n' @printf '*****************\n' pytest .PHONY: unittest unittest: @printf '\n\n*****************\n' @printf '$(color)Running unittest$(off)\n' @printf '*****************\n' PYTHONPATH=$(shell pwd) $(PYTHON) tests/test_unittest.py .PHONY: doctest doctest: @printf '\n\n*****************\n' @printf '$(color)Running doctest$(off)\n' @printf '*****************\n' $(PYTHON) tests/test_doctest.py .PHONY: mypy mypy: @printf '\n\n*****************\n' @printf '$(color)Running mypy$(off)\n' @printf '*****************\n' mypy ${TARGETS} .PHONY: isort isort: @printf '\n\n*****************\n' @printf '$(color)Running isort$(off)\n' @printf '*****************\n' isort --check-only ${TARGETS} .PHONY: black black: @printf '\n\n*****************\n' @printf '$(color)Running black$(off)\n' @printf '*****************\n' black --check ${TARGETS} .PHONY: pylint pylint: @printf '\n\n*****************\n' @printf '$(color)Running pylint$(off)\n' @printf '*****************\n' pylint ${TARGETS} .PHONY: docs docs: @printf '\n\n*****************\n' @printf '$(color)Test building docs$(off)\n' @printf '*****************\n' mkdocs build --strict flexmock-0.13.0/README.md 0000664 0000000 0000000 00000007663 15130526717 0014705 0 ustar 00root root 0000000 0000000
flexmock - Mock, stub, and spy library for Python.
--- Flexmock is a testing library for Python that makes it easy to create mocks, stubs, and fakes. ## Features - **Mock**: Easily create mock objects and make assertions about which methods or attributes were used and arguments they were called with. - **Spy**: Proxy calls to object's original methods or attributes and make assertions based on return values or call count. - **Fake**: Generate a fake objects to be used in your tests with ease. - **Stub**: Create stub objects which replace parts of existing objects and classes with just one call. - **No external dependencies**: Flexmock is lightweight and only uses Python standard library. There are no external dependencies. - **Simple and intuitive**: Declarations are structured to read more like English sentences than API calls, so they are easy to learn and use. - **Fully type annotated**: External API is fully type annotated so it works great with static analysis tools and editor auto-completion. - **Integrations with test runners**: Integrates seamlessly with all major test runners like unittest, doctest, and pytest. - **Python 3.10+ and PyPy3**: Extensively tested to work with latest Python versions. ## Installation Install with pip: ``` pip install flexmock ``` ## Examples Flexmock features smooth integration with pretty much every popular test runner, so no special setup is necessary. Simply importing flexmock into your test module is sufficient to get started with any of the following examples: ```python from flexmock import flexmock ``` ### Mocks Assertions take many flavors and flexmock has many different facilities to generate them: ```python # Simplest is ensuring that a certain method is called flexmock(Train).should_receive("get_tickets").once() # Of course, it is also possible to provide a default return value flexmock(Train).should_receive("get_destination").and_return("Paris").once() # Or check that a method is called with specific arguments flexmock(Train).should_receive("set_destination").with_args("Seoul").at_least().twice() ``` ### Spies Instead of mocking, there are also times when you want to execute the actual method and simply find out how many times it was called. Flexmock uses `should_call` to generate this sort of assertions instead of `should_receive`: ```python # Verify that a method is called at most three times flexmock(Train).should_call("get_tickets").at_most().times(3) # Make sure that a method is never called with specific arguments flexmock(Train).should_call("set_destination").with_args("Helsinki").never() # More complex example with features like argument type and exception matching flexmock(Train).should_call("crash").with_args(str, int).and_raise(AttributeError).once() ``` See more examples in the documentation. ## Documentation User guide, examples, and a full API reference is available at: https://flexmock.readthedocs.io ## Contributing Contributions are absolutely welcome and encouraged! See [CONTRIBUTING.md](https://github.com/flexmock/flexmock/blob/master/CONTRIBUTING.md) to get started. flexmock-0.13.0/docs/ 0000775 0000000 0000000 00000000000 15130526717 0014342 5 ustar 00root root 0000000 0000000 flexmock-0.13.0/docs/advanced/ 0000775 0000000 0000000 00000000000 15130526717 0016107 5 ustar 00root root 0000000 0000000 flexmock-0.13.0/docs/advanced/builtin_functions.md 0000664 0000000 0000000 00000001311 15130526717 0022163 0 ustar 00root root 0000000 0000000 # Mocking builtins Mocking or stubbing out builtin functions, such as `open()`, can be slightly tricky. It is not always obvious when the builtin function you are trying to mock might be internally called by the test runner and cause unexpected behavior in the test. As a result, the recommended way to mock out builtin functions is to always specify a fall-through with `should_call()` first and use `with_args()` to limit the scope of your mock or stub to just the specific invocation you are trying to replace: ```python mock = flexmock(__builtins__) mock.should_call("open") # set the fall-through mock.should_receive("open").with_args("file_name").and_return( flexmock(read=lambda: "some data") ) ``` flexmock-0.13.0/docs/advanced/call_order.md 0000664 0000000 0000000 00000001173 15130526717 0020541 0 ustar 00root root 0000000 0000000 # Asserting call order Flexmock does not enforce call order by default, but it's easy to do if you need to: ```python flexmock(plane).should_receive("fly").with_args("left").and_return( "ok" ).ordered() flexmock(plane).should_receive("fly").with_args("right").and_return( "ok" ).ordered() ``` The order of the flexmock calls is the order in which these methods will need to be called by the code under test. If method `fly()` above is called with the right arguments in the declared order things will be fine and both will return `"ok"`. But trying to call `fly("right")` before `fly("left")` will result in an exception. flexmock-0.13.0/docs/advanced/chained_methods.md 0000664 0000000 0000000 00000003466 15130526717 0021560 0 ustar 00root root 0000000 0000000 # Chained methods Let's say you have some code that looks something like the following: ```python http = HTTP() results = http.get_url("http://www.google.com").parse().display_results() ``` You could use flexmock to mock each of these method calls individually: ```python mock = flexmock( get_url=lambda: flexmock(parse=lambda: flexmock(display_results="ok")) ) flexmock(HTTP).new_instances(mock) ``` But that looks really error prone and quite difficult to read. Here is a better way: ```python mock = flexmock() flexmock(HTTP).new_instances(mock) mock.should_receive("get_url.parse.display_results").and_return("ok") ``` When using this short-hand, flexmock will create intermediate objects and expectations, returning the final one in the chain. As a result, any further modifications, such as `with_args()` or `times()` modifiers, will only be applied to the final method in the chain. If you need finer grained control, such as specifying specific arguments to an intermediate method, you can always fall back to the above long version. Word of caution: because flexmock generates temporary intermediate mock objects for each step along the chain, trying to mock two method call chains with the same prefix will not work. That is, doing the following will fail to set up the stub for `display_results()` because the one for `save_results()` overrides it: ```python flexmock(HTTP).should_receive("get_url.parse.display_results").and_return("ok") flexmock(HTTP).should_receive("get_url.parse.save_results").and_return("ok") ``` In this situation, you should identify the point where the chain starts to diverge and return a flexmock object that handles all the "tail" methods using the same object: ```python flexmock(HTTP).should_receive("get_url.parse").and_return( flexmock(display_results="ok", save_results="ok") ) ``` flexmock-0.13.0/docs/advanced/generators.md 0000664 0000000 0000000 00000001174 15130526717 0020605 0 ustar 00root root 0000000 0000000 # Generators In addition to returning values and raising exceptions, flexmock can also turn the mocked method into a generator that yields successive values: ```python >>> flexmock(plane).should_receive("flight_log").and_yield( "take off", "flight", "landing") >>> for i in plane.flight_log(): >>> print(i) "take off" "flight" "landing" ``` You can also use Python's builtin `iter()` function to generate an iterable return value: ```python flexmock(plane, flight_log=iter(["take off", "flight", "landing"])) ``` In fact, the `and_yield()` modifier is just shorthand for `should_receive().and_return(iter)` under the hood. flexmock-0.13.0/docs/advanced/multiple_return.md 0000664 0000000 0000000 00000001224 15130526717 0021662 0 ustar 00root root 0000000 0000000 # Multiple return values It is possible for the mocked method to return different values on successive calls: ```python >>> flexmock(group).should_receive("get_member").and_return("user1").and_return( "user2" ).and_return("user3") >>> group.get_member() "user1" >>> group.get_member() "user2" >>> group.get_member() "user3" ``` Or use the short-hand form ```python flexmock(group).should_receive("get_member").and_return( "user1", "user2", "user3" ).one_by_one() ``` You can also mix return values with exception raises ```python flexmock(group).should_receive("get_member").and_return("user1").and_raise( Exception ).and_return("user2") ``` flexmock-0.13.0/docs/advanced/new_instances.md 0000664 0000000 0000000 00000003333 15130526717 0021273 0 ustar 00root root 0000000 0000000 # Fake new instances Occasionally you will want a class to create fake objects when it is being instantiated. Flexmock makes it easy and painless. Your first option is to simply replace the class with a function: ```python flexmock(some_module).should_receive("NameOfClass").and_return(fake_instance) # fake_instance can be created with flexmock as well ``` The downside if this is that you may run into subtle issues since the class has now been replaced by a function. Flexmock offers another alternative using the `.new_instances()` method: !!!warning Usage of `.new_instances()` method is discouraged due to a bug in CPython which prevents proper teardown of the mock. Due to this bug, the mock leaks into other tests and can prevent creating new instances of the class. More information in [issue #16](https://github.com/flexmock/flexmock/issues/16). ```python >>> class Group: pass >>> fake_group = flexmock(name="fake") >>> flexmock(Group).new_instances(fake_group) >>> Group().name == "fake" True ``` It is also possible to return different fake objects in a sequence: ```python >>> class Group: pass >>> fake_group1 = flexmock(name="fake") >>> fake_group2 = flexmock(name="real") >>> flexmock(Group).new_instances(fake_group1, fake_group2) >>> Group().name == "fake" True >>> Group().name == "real" True ``` Another approach, if you're familiar with how instance instatiation is done in Python, is to stub the `__new__` method directly: ```python >>> flexmock(Group).should_receive("__new__").and_return(fake_group) >>> # or, if you want to be even slicker >>> flexmock(Group, __new__=fake_group) ``` In fact, the new_instances command is simply shorthand for `should_receive("__new__").and_return()` under the hood. flexmock-0.13.0/docs/advanced/replacing_functions.md 0000664 0000000 0000000 00000001311 15130526717 0022461 0 ustar 00root root 0000000 0000000 # Replacing methods There are times when it is useful to replace a method with a custom lambda or function, rather than simply stubbing it out, in order to return custom values based on provided arguments or a global value that changes between method calls. ```python flexmock(plane).should_receive("set_speed").replace_with(lambda x: x == 5) ``` There is also shorthand for this, similar to the shorthand for `should_receive`/`and_return`: ```python flexmock(plane, set_speed=lambda x: x == 5) ``` !!!note Whenever the return value provided to the key=value shorthand is a callable (such as lambda), flexmock expands it to `should_receive().replace_with()` rather than `should_receive().and_return().` flexmock-0.13.0/docs/advanced/state.md 0000664 0000000 0000000 00000002404 15130526717 0017551 0 ustar 00root root 0000000 0000000 # Conditional assertions Flexmock supports conditional assertions based on external state. Consider the rather contrived `Radio` class with the following methods: ```python class Radio: is_on = False def __init__(self): self.volume = 0 def switch_on(self): self.is_on = True def switch_off(self): self.is_on = False def select_channel(self): return None def adjust_volume(self, num): self.volume = num radio = Radio() ``` Now we can define some method call expectations dependent on the state of the radio: ```python flexmock(radio) radio.should_receive("select_channel").when(lambda: radio.is_on).once() radio.should_call("adjust_volume").with_args(5).when(lambda: radio.is_on).once() ``` Calling these while the radio is off will result in an error: ```python radio.select_channel() # Traceback (most recent call last): # flexmock.StateError: select_channel expected to be called # when condition is True ``` ```python radio.adjust_volume(5) # Traceback (most recent call last): # flexmock.StateError: adjust_volume expected to be called # when condition is True ``` Turning the radio on will make things work as expected: ```python radio.is_on = True radio.select_channel() radio.adjust_volume(5) ``` flexmock-0.13.0/docs/api.md 0000664 0000000 0000000 00000000124 15130526717 0015432 0 ustar 00root root 0000000 0000000 # API reference ::: flexmock.flexmock ::: flexmock.Mock ::: flexmock.Expectation flexmock-0.13.0/docs/assets/ 0000775 0000000 0000000 00000000000 15130526717 0015644 5 ustar 00root root 0000000 0000000 flexmock-0.13.0/docs/assets/extra.css 0000664 0000000 0000000 00000000267 15130526717 0017506 0 ustar 00root root 0000000 0000000 /* mkdocstrings */ div.doc-contents:not(.first) { padding-left: 25px; border-left: 4px solid rgba(230, 230, 230); margin-bottom: 80px; } .md-typeset h2 { font-weight: 400; } flexmock-0.13.0/docs/assets/icon_black.png 0000664 0000000 0000000 00000006431 15130526717 0020442 0 ustar 00root root 0000000 0000000 PNG IHDR >a sBIT|d IDATx^wuG{ Db{aW+E!V?AP"+[bEEb]~ݻgٝ}7h8~ս?n ` XVa VwXk`` `Xy XVa 5%ћ+Iuy^'t۞/\*մhn}B~:)E\-u+ TW + USC7ԵψPZϫS>p]?K|<#=]K>'fZȥ-]B6 xkS_}D1sE> 9dENIk | xz٢gzB LLLdoVXZ~ `?+ ^z.).<7RaRz W{{R*,-4 ! $~k%