def test_it_supports_multiple_returns(self): die = stub() die.expects.roll().and_returns(2) die.expects.roll().and_returns(3) self.assertEqual(die(), 2) self.assertEqual(die(), 3) self.assertEqual(die(), 3)
def test_it_supports_exceptions(self): die = stub() die.expects.roll().and_raises(TypeError) with self.assertRaises(TypeError): die.roll() with self.assertRaises(TypeError): die.roll()
def test_it_accepts_any_arg(self): die = stub() die.expects.roll(flags.ANY_ARG).and_returns(3) self.assertEqual(die.roll(1), 3) self.assertEqual(die.roll(2), 3) self.assertTrue(isinstance(die.roll(1, 2), Mock)) # => stub instance
def test_it_supports_functions(self): die = stub() die.expects.roll().and_calls(lambda x: 1, lambda x: x) self.assertEqual(die.roll(6), 1) self.assertEqual(die.roll(6), 6) self.assertEqual(die.roll(5), 5)
def test_it_supports_yielding(self): die = stub() die.expects.roll().and_yields(2, 3) self.assertEqual(list(die.roll()), [2, 3])
def test_custom_equality(self): die = stub() die.expects.__eq__(2).and_returns(True) die.expects.__eq__(1).and_returns(False) self.assertEqual(die, 2) self.assertNotEqual(die, 1)
def test_it_accepts_anything(self): die = stub() die.expects.roll(flags.ANYTHING).and_returns(3) self.assertEqual(die.roll(3, 4, 5, 6), 3) self.assertEqual(die.roll(foo="bar"), 3) self.assertEqual(die.roll("the cake", is_a="lie"), 3)
def test_it_accepts_variable_args_and_kwargs(self): die = stub() die.expects.roll(flags.ANY_ARGS, flags.ANY_KWARGS).and_returns(3) self.assertEqual(die.roll(3, 4, 5, 6), 3) self.assertEqual(die.roll(foo="bar"), 3) self.assertEqual(die.roll("the cake", is_a="lie"), 3)