Example #1
0
    def test_spy_raises(self):
        s = Spy()
        s.raises(ValueError("Text"))
        with self.assertRaises(ValueError) as e:
            s()

        self.assertEqual(str(e.exception), 'Text')
Example #2
0
 def test_call_count(self):
     s = Spy('str')
     s.upper()
     self.assertEqual(1, s.upper.call_count())
     s.lower()
     self.assertEqual(1, s.upper.call_count())
     s.upper()
     self.assertEqual(2, s.upper.call_count())
Example #3
0
    def test_raises_with_condition(self):
        wrapper = AttributeWrapper('name', Spy())
        wrapper.raises_if_args(lambda x: x == 2, ValueError, 'Message')
        with self.assertRaises(ValueError) as e:
            wrapper(2)

        self.assertEqual(str(e.exception), 'Message')
Example #4
0
    def test_raises(self):
        wrapper = AttributeWrapper('name', Spy())
        wrapper.raises(ValueError, 'Message')
        with self.assertRaises(ValueError) as e:
            wrapper()

        self.assertEqual(str(e.exception), 'Message')
Example #5
0
 def test_str(self):
     wrapper = AttributeWrapper('name', Spy())
     self.assertTrue('Wrapper for method "name" on object:' in str(wrapper))
Example #6
0
 def test_was_called(self):
     wrapper = AttributeWrapper('name', Spy())
     self.assertFalse(wrapper.was_called())
     wrapper()
     self.assertTrue(wrapper.was_called())
Example #7
0
 def test_returns_value_if_specified_after_function(self):
     wrapper = AttributeWrapper('name', Spy())
     wrapper.use_function(bool)
     wrapper.returns(False)
     self.assertFalse(wrapper(1))
Example #8
0
 def test_returns(self):
     wrapper = AttributeWrapper('name', Spy())
     wrapper.returns(5)
     self.assertEqual(5, wrapper())
Example #9
0
 def test_returns_when_func_called(self):
     spy = Spy('string')
     spy.upper.returns(22)
     self.assertEqual(22, spy.upper())
Example #10
0
 def test_can_call_empty_spy(self):
     spy = Spy()
     spy()
Example #11
0
 def test_create_spy_on_str(self):
     s = 'string'
     spy = Spy(s)
Example #12
0
 def test_create_spy_empty(self):
     spy = Spy()
Example #13
0
 def test_spy_isinstance_for_empty(self):
     spy = Spy()
     self.assertTrue(isinstance(spy, type(None)))
Example #14
0
 def test_spy_isinstance(self):
     spy = Spy("sds")
     self.assertTrue(isinstance(spy, str))
     self.assertFalse(isinstance(spy, int))
     self.assertFalse(isinstance('text', int))
     self.assertTrue(isinstance(11, int))
Example #15
0
 def test_spy_on_stringio(self):
     spy = Spy(StringIO('1'))
     spy.write.returns(10)
     self.assertEqual(10, spy.write())
Example #16
0
 def test_was_func_called_with_arg(self):
     spy = Spy('string')
     spy.upper(1)
     self.assertTrue(spy.was_function_with_argument_called('upper', 1))
Example #17
0
 def test_was_exact_func_called(self):
     spy = Spy('string')
     spy.upper(1)
     self.assertTrue(spy.was_exact_function_called('upper', 1))
Example #18
0
 def test_can_call_str_func_on_spy(self):
     s = "string"
     spy = Spy(s)
     spy.upper()
Example #19
0
 def test_default_returns_none(self):
     wrapper = AttributeWrapper('name', Spy())
     self.assertIsNone(wrapper())
Example #20
0
 def test_was_called_works(self):
     spy = Spy()
     self.assertFalse(spy.was_called())
     spy()
     self.assertTrue(spy.was_called())
Example #21
0
 def test_returns_function_result(self):
     wrapper = AttributeWrapper('name', Spy())
     wrapper.use_function(bool)
     self.assertTrue(wrapper(1))
Example #22
0
 def test_was_called_empty_with_arg(self):
     spy = Spy()
     spy(10)
     self.assertTrue(spy.was_called())
     self.assertTrue(spy.was_called_with_argument(10))
Example #23
0
 def test_returns_on_called_empty(self):
     spy = Spy()
     spy.returns(11)
     self.assertEqual(11, spy())
Example #24
0
 def test_str_on_empty(self):
     spy = Spy()
     self.assertEqual('Empty Test Spy', str(spy))
Example #25
0
 def test_raises_with_condition_not_raise_if_no_arg(self):
     wrapper = AttributeWrapper('name', Spy())
     wrapper.raises_if_args(lambda x: x == 2, ValueError, 'Message')
     wrapper()
Example #26
0
 def test_str_(self):
     spy = Spy('string')
     self.assertEqual("Test spy of the \"string\" <class 'str'>", str(spy))
Example #27
0
 def test_call_count(self):
     wrapper = AttributeWrapper('name', Spy())
     self.assertEqual(0, wrapper.call_count())
     wrapper()
     self.assertEqual(1, wrapper.call_count())
Example #28
0
 def test_all_calls(self):
     spy = Spy('string')
     self.assertEqual([], spy.all_calls())
     spy.upper()
     self.assertTrue(len(spy.all_calls()) == 1)
Example #29
0
 def test_all_calls(self):
     wrapper = AttributeWrapper('name', Spy())
     self.assertEqual([], wrapper.all_calls())
     wrapper()
     self.assertEqual([((), {})], wrapper.all_calls())
Example #30
0
 def test_all_calls_args_flatten(self):
     s = Spy('str')
     s.replace('t', 'T')
     s.upper()
     s.lower(1)
     self.assertEqual(['t', 'T', 1], s.all_calls_args_flatten())