def test_call_method_can_raise_an_exception(): '''Substitute: How to raise exceptions''' # System under Test substitute = Substitute() # Act: Configure substitute.method.raises(Apology) # Act: Run substitute.method()
def test_call_method_on_substitute(): '''Substitute: How to verify that a method was called''' # System under Test substitute = Substitute() # Act: Run substitute.method() # Assert assert_true(substitute.method.was_called) assert_false(substitute.other_method.was_called)
def test_call_intercepts_argument_tuples(): '''Substitute: How to intercept method arguments''' # System under Test substitute = Substitute() # Act: Run substitute.method(0, []) # Assert assert_equal(substitute.method.args, (0, [])) assert_true(substitute.method.was_called_with(0, []))
def test_call_intercepts_several_arguments(): '''Substitute: When a method was called several times, all arguments are stored''' # System under Test substitute = Substitute() # Act: Run substitute.method('hello') substitute.method('bye') # Assert assert_true(substitute.method.was_called_with('hello')) assert_true(substitute.method.was_called_with('bye'))
def test_calls_are_counted(): '''Substitute: How to verify that a method was called several times''' # System under Test substitute = Substitute() # Arrange substitute.method() assert_true(substitute.method.was_called) assert_false(substitute.method.was_called_times(2)) # Act: Run substitute.method() # Assert assert_true(substitute.method.was_called) assert_true(substitute.method.was_called_times(2))
def test_call_method_returns_none(): '''Substitute: How to call a method on a substitute''' # System under Test substitute = Substitute() # Act: Run data = substitute.method() # Assert assert_equal(data, None)
def test_call_intercepts_many_arguments(): '''Substitute: When a call had many arguments, they are returned as a tuple''' # System under Test substitute = Substitute() # Act: Run data = substitute.method('hello', 1, 2, 3) # Assert assert_equal(substitute.method.args, ('hello', 1, 2, 3))
def test_call_intercepts_single_argument(): '''Substitute: How to intercept the arguments of a call''' # System under Test substitute = Substitute() # Act: Run data = substitute.method('hello') # Assert assert_equal(substitute.method.args, 'hello')
def test_call_method_can_return_data(): '''Substitute: How to provide data from a method''' # System under Test substitute = Substitute() # Act: Configure substitute.method.returns(1) # Act: Run data = substitute.method() # Assert assert_equal(data, 1)
def test_call_intercepts_asserts_arguments(): '''Substitute: How to verify that a method was called with specific arguments''' # System under Test substitute = Substitute() # Act: Configure substitute.method.returns(1) # Act: Run data = substitute.method('hello') # Assert assert_true(substitute.method.was_called_with('hello')) assert_false(substitute.method.was_called_with('bye'))