Beispiel #1
0
    def test_explict_declaration_of_raise_exception(self):
        obj = TempClass()

        spyOn(obj, 'hello')
        obj.hello.raiseException = Exception

        self.assertRaises(Exception, obj.hello)
Beispiel #2
0
    def test_most_recent_call_kwargs(self):
        obj = TempClass()
        spyOn(obj, 'hello')

        obj.hello(say="hello")

        self.assertEqual(obj.hello.mostRecentCall.kwargs, dict(say="hello"))
Beispiel #3
0
    def test_was_called_after_call(self):
        obj = TempClass()
        spyOn(obj, 'hello')

        obj.hello()

        self.assertTrue(obj.hello.wasCalled())
Beispiel #4
0
    def test_stop(self):
        obj = TempClass()
        spyOn(obj, 'hello', returnValue="spy says hello")

        spy.Method.stop()

        self.assertEqual(obj.hello(), "hello")
Beispiel #5
0
    def test_explicit_return_value(self):
        obj = TempClass()

        spyOn(obj, 'hello')
        obj.hello.returnValue = "spy says hello"

        self.assertEqual(obj.hello(), "spy says hello")
Beispiel #6
0
    def test_most_recent_call_args(self):
        obj = TempClass()
        spyOn(obj, 'hello')

        obj.hello("h", "e", "l", "l", "o")

        self.assertEqual(obj.hello.mostRecentCall.args,
                        ("h", "e", "l", "l", "o"))
Beispiel #7
0
    def test_call_count_after_call(self):
        obj = TempClass()
        spyOn(obj, 'hello')

        obj.hello()
        obj.hello()

        self.assertEqual(obj.hello.callCount, 2)
Beispiel #8
0
    def test_was_called_times(self):
        obj = TempClass()
        spyOn(obj, 'hello')

        obj.hello()
        obj.hello()

        self.assertTrue(obj.hello.wasCalled(times=2))
Beispiel #9
0
    def test_toHaveBeenCalledTimes(self):
        obj = TempClass()
        spyOn(obj, "hello")

        obj.hello()
        obj.hello()

        expect(obj.hello).toHaveBeenCalled(times=2)
        self.assertRaises(AssertionError, expect(obj.hello).toHaveBeenCalled, times=1)
Beispiel #10
0
    def test_kwargs_for_call(self):
        obj = TempClass()
        spyOn(obj, 'hello')

        obj.hello(say="hello")
        obj.hello(to="world")

        self.assertEqual(obj.hello.kwargsForCall(0), dict(say="hello"))
        self.assertEqual(obj.hello.kwargsForCall(1), dict(to="world"))
Beispiel #11
0
    def test_args_for_call(self):
        obj = TempClass()
        spyOn(obj, 'hello')

        obj.hello("h", "e")
        obj.hello("l", "l", "o")

        self.assertEqual(obj.hello.argsForCall(0), ("h", "e"))
        self.assertEqual(obj.hello.argsForCall(1), ("l", "l", "o"))
Beispiel #12
0
    def test_method_chaining(self):
        obj = TempClass()
        spyOn(obj, 'hello')

        obj.hello().world().wassup()

        self.assertEqual(obj.hello.callCount, 1)
        self.assertEqual(obj.hello().world.callCount, 1)
        self.assertEqual(obj.hello().world().wassup.callCount, 1)
Beispiel #13
0
    def test_call_through_for_andCallThrough_syntax(self):
        obj = TempClass()

        spyOn(obj, 'hello').andCallThrough()
        spyOn(obj, 'hello_world').andCallThrough()

        self.assertEqual(obj.hello(), "hello")
        self.assertEqual(obj.hello.callCount, 1)
        self.assertEqual(obj.hello_world("hello world"), "hello world")
        self.assertEqual(obj.hello_world.callCount, 1)
Beispiel #14
0
    def test_call_through(self):
        obj = TempClass()

        spyOn(obj, 'hello', callThrough=True)
        spyOn(obj, 'hello_world', callThrough=True)

        self.assertEqual(obj.hello(), "hello")
        self.assertEqual(obj.hello.callCount, 1)
        self.assertEqual(obj.hello_world("hello world"), "hello world")
        self.assertEqual(obj.hello_world.callCount, 1)
Beispiel #15
0
    def test_was_called_with(self):
        obj = TempClass()
        spyOn(obj, 'hello')

        obj.hello(1)
        obj.hello(1, to=2)

        self.assertTrue(obj.hello.wasCalledWith(1))
        self.assertTrue(obj.hello.wasCalledWith(1, to=2))
        self.assertFalse(obj.hello.wasCalledWith(2))
Beispiel #16
0
    def test_was_called_before(self):
        obj = TempClass()
        spyOn(obj, 'hello')
        spyOn(obj, 'hi')

        obj.hi()
        obj.hello()

        self.assertTrue(obj.hi.wasCalledBefore(obj.hello))
        self.assertFalse(obj.hello.wasCalledBefore(obj.hi))
Beispiel #17
0
    def test_toHaveBeenCalled(self):
        obj = TempClass()
        spyOn(obj, "hello")
        spyOn(obj, "hi")
        obj.hello()

        expect(obj.hello).toHaveBeenCalled()
        expect(obj.hi).notToHaveBeenCalled()

        self.assertRaises(AssertionError, expect(obj.hello).notToHaveBeenCalled)
        self.assertRaises(AssertionError, expect(obj.hi).toHaveBeenCalled)
Beispiel #18
0
    def test_toHaveBeenCalledBefore(self):
        obj = TempClass()
        spyOn(obj, 'hello')
        spyOn(obj, 'hi')

        obj.hi()
        obj.hello()

        expect(obj.hi).toHaveBeenCalledBefore(obj.hello)
        expect(obj.hello).notToHaveBeenCalledBefore(obj.hi)

        self.assertRaises(AssertionError,
                          expect(obj.hi).notToHaveBeenCalledBefore, obj.hello)
        self.assertRaises(AssertionError,
                          expect(obj.hello).toHaveBeenCalledBefore, obj.hi)
Beispiel #19
0
    def test_spy_is_rolled_back_outside_the_with_block(self):
        obj = TempClass()

        with spyOn(obj, 'hello', returnValue="spy says hello"):
            pass

        self.assertEqual(obj.hello(), "hello")
Beispiel #20
0
    def test_toHaveBeenCalledWith(self):
        obj = TempClass()
        spyOn(obj, "hello")
        spyOn(obj, "hi")
        obj.hello(1, to=2)

        expect(obj.hello).toHaveBeenCalledWith(1, to=2)
        expect(obj.hello).toHaveBeenCalledWith(1, to=any(int))
        expect(obj.hello).toHaveBeenCalledWith(any(int), to=2)
        expect(obj.hello).toHaveBeenCalledWith(any(int), to=any(int))
        expect(obj.hi).notToHaveBeenCalledWith(1, to=2)
        expect(obj.hi).notToHaveBeenCalledWith(any(bool), to=any(bool))

        self.assertRaises(AssertionError, expect(obj.hello).notToHaveBeenCalledWith, 1, to=2)
        self.assertRaises(AssertionError, expect(obj.hello).notToHaveBeenCalledWith, any(int), to=any(int))
        self.assertRaises(AssertionError, expect(obj.hi).toHaveBeenCalledWith, 1, to=2)
        self.assertRaises(AssertionError, expect(obj.hi).toHaveBeenCalledWith, any(bool), to=any(bool))
Beispiel #21
0
    def test_explicit_call_fake(self):
        obj = TempClass()

        def fake_hello():
            return "fake hello"

        def fake_hello_world(message):
            return message

        class FakeTempClass(object):
            def test(self, message):
                return message

        spyOn(obj, 'hello')
        obj.hello.callFake = fake_hello
        spyOn(obj, 'hello_world')
        obj.hello_world.callFake = fake_hello_world
        spyOn(obj, 'test')
        obj.test.callFake = FakeTempClass().test

        self.assertEqual(obj.hello(), "fake hello")
        self.assertEqual(obj.hello.callCount, 1)
        self.assertEqual(obj.hello_world("hello world"), "hello world")
        self.assertEqual(obj.hello_world.callCount, 1)
        self.assertEqual(obj.test("test"), "test")
        self.assertEqual(obj.hello_world.callCount, 1)
Beispiel #22
0
    def test_call_number(self):
        obj = TempClass()
        spyOn(obj, 'hello')
        spyOn(obj, 'hi')
        spyOn(obj, 'namaste')

        obj.hi()
        obj.namaste()
        obj.hello()

        self.assertEqual(obj.hi.callNumber, 1)
        self.assertEqual(obj.namaste.callNumber, 2)
        self.assertEqual(obj.hello.callNumber, 3)
Beispiel #23
0
    def test_call_fake_for_andCallFakeSyntax(self):
        obj = TempClass()

        def fake_hello():
            return "fake hello"

        def fake_hello_world(message):
            return message

        class FakeTempClass(object):
            def test(self, message):
                return message

        spyOn(obj, 'hello').andCallFake(fake_hello)
        spyOn(obj, 'hello_world').andCallFake(fake_hello_world)
        spyOn(obj, 'hello_world').andCallFake(FakeTempClass().test)

        self.assertEqual(obj.hello(), "fake hello")
        self.assertEqual(obj.hello.callCount, 1)
        self.assertEqual(obj.hello_world("hello world"), "hello world")
        self.assertEqual(obj.hello_world.callCount, 1)
        self.assertEqual(obj.test("test"), "test")
        self.assertEqual(obj.hello_world.callCount, 1)
Beispiel #24
0
    def test_class_decorator_stops_spying_after_running_the_test(self):
        pungi.spyOn(self.obj, 'hello', returnValue="spy says hello")

        self.assertEqual(self.obj.hello(), "spy says hello")
Beispiel #25
0
    def test_raises_exception(self):
        obj = TempClass()

        spyOn(obj, 'hello', raiseException=Exception)

        self.assertRaises(Exception, obj.hello)
Beispiel #26
0
    def test_raises_exception_for_andRaise_syntax(self):
        obj = TempClass()

        spyOn(obj, 'hello').andRaise(Exception)

        self.assertRaises(Exception, obj.hello)
Beispiel #27
0
    def test_was_called_before_call(self):
        obj = TempClass()

        spyOn(obj, 'hello')

        self.assertFalse(obj.hello.wasCalled())
Beispiel #28
0
    def test_call_count_before_call(self):
        obj = TempClass()
        spyOn(obj, 'hello')

        self.assertEqual(obj.hello.callCount, 0)
Beispiel #29
0
    def test_return_value_for_andReturn_syntax(self):
        obj = TempClass()

        spyOn(obj, 'hello').andReturn("spy says hello")

        self.assertEqual(obj.hello(), "spy says hello")
Beispiel #30
0
    def test_repr(self):
        obj = TempClass()
        spyOn(obj, 'hello')

        self.assertEqual(repr(obj.hello), "{0}.hello".format(repr(obj)))