def scope_mocks(): with zest.mock(pretend_unit_under_test.foo) as m_foo: pretend_unit_under_test.foo() # Had the real foo been called it would have # raised NotImplementedError assert m_foo.called_once()
def it_exceptions_serially(): with zest.mock(pretend_unit_under_test.foo) as m_foo: m_foo.exceptions_serially([ValueError, TypeError]) with zest.raises(ValueError): pretend_unit_under_test.foo() with zest.raises(TypeError): pretend_unit_under_test.foo()
def it_normalizes_calls_into_kwargs(): # normalized_call() is a handy when you want to just know # what was passed to the mock but you don't care if # it was passed as args or kwargs. with zest.mock(pretend_unit_under_test.foo) as m_foo: pretend_unit_under_test.foo("arg1", arg2="arg2") kwargs = m_foo.normalized_call() assert kwargs == dict(arg1="arg1", arg2="arg2")
def it_hooks(): with zest.mock(pretend_unit_under_test.foo) as m_foo: got_callback = False def _callback(): nonlocal got_callback got_callback = True m_foo.hook(_callback) pretend_unit_under_test.foo() assert got_callback is True
def it_checks_against_normalized_call(): with zest.mock(pretend_unit_under_test.foo) as m_foo: pretend_unit_under_test.foo("arg1", arg2="arg2") assert m_foo.called_once_with_kws(arg1="arg1", arg2="arg2")
def it_exceptions(): with zest.mock(pretend_unit_under_test.foo) as m_foo: m_foo.exceptions(ValueError) with zest.raises(ValueError): pretend_unit_under_test.foo()
def it_returns_serial_values(): with zest.mock(pretend_unit_under_test.foo) as m_foo: m_foo.returns_serially([1, 2]) assert pretend_unit_under_test.foo() == 1 assert pretend_unit_under_test.foo() == 2
def it_returns_value(): with zest.mock(pretend_unit_under_test.foo) as m_foo: m_foo.returns(1) assert pretend_unit_under_test.foo() == 1
def it_resets(): with zest.mock(pretend_unit_under_test.foo) as m_foo: pretend_unit_under_test.foo() m_foo.reset() pretend_unit_under_test.foo() assert m_foo.n_calls == 1
def it_counts_n_calls(): with zest.mock(pretend_unit_under_test.foo) as m_foo: pretend_unit_under_test.foo() pretend_unit_under_test.foo() assert m_foo.n_calls == 2
def test_1(): pretend_unit_under_test.foo() assert m_foo.called_once()