コード例 #1
0
 def test233_onThirdCall_random_args(self):
     fto = ForTestOnly()
     stub = SinonStub(ForTestOnly, "func1")
     stub.onThirdCall().returns("onThirdCall")
     self.assertEqual(fto.func1(), None)
     self.assertEqual(fto.func1(), None)
     self.assertEqual(fto.func1(1), "onThirdCall")
コード例 #2
0
 def test392_chained_function_throws(self):
     stub = SinonStub(C_func)
     stub.withArgs(42).onFirstCall().throws(
         Exception('A')).onSecondCall().throws(Exception('B'))
     with self.assertRaisesRegexp(Exception, 'A'):
         stub.g.C_func(42)
     with self.assertRaisesRegexp(Exception, 'B'):
         stub.g.C_func(42)
コード例 #3
0
 def test226_throws_with_condition(self):
     fto = ForTestOnly()
     self.assertEqual(fto.func1(), "func1")
     stub = SinonStub(ForTestOnly, "func1")
     stub.onSecondCall().throws()
     fto.func1()
     with self.assertRaises(Exception) as context:
         fto.func1()
コード例 #4
0
 def test442_withArgs_onCall_returns_does_not_reset_call_count(self):
     stub = SinonStub()
     self.assertEqual(stub(), None)
     self.assertEqual(stub('A'), None)
     stub.withArgs('A').onSecondCall().returns('second call!')
     self.assertEqual(stub('A'), 'second call!')
     self.assertEqual(stub('A'), None)
     self.assertEqual(stub(), None)
コード例 #5
0
 def test391_chained_module_throws(self):
     stub = SinonStub(os, 'system')
     stub.withArgs(42).onFirstCall().throws(
         Exception('A')).onSecondCall().throws(Exception('B'))
     with self.assertRaisesRegexp(Exception, 'A'):
         os.system(42)
     with self.assertRaisesRegexp(Exception, 'B'):
         os.system(42)
コード例 #6
0
 def test390_chained_pure_throws(self):
     stub = SinonStub()
     stub.withArgs(42).onFirstCall().throws(
         Exception('A')).onSecondCall().throws(Exception('B'))
     with self.assertRaisesRegexp(Exception, 'A'):
         stub(42)
     with self.assertRaisesRegexp(Exception, 'B'):
         stub(42)
コード例 #7
0
 def test393_chained_method_throws(self):
     o = A_object()
     stub = SinonStub(o, 'A_func')
     stub.withArgs(42).onFirstCall().throws(
         Exception('A')).onSecondCall().throws(Exception('B'))
     with self.assertRaisesRegexp(Exception, 'A'):
         o.A_func(42)
     with self.assertRaisesRegexp(Exception, 'B'):
         o.A_func(42)
コード例 #8
0
 def test443_withArgs_onCall_throws_does_not_reset_call_count(self):
     stub = SinonStub()
     self.assertEqual(stub(), None)
     self.assertEqual(stub('A'), None)
     stub.withArgs('A').onSecondCall().throws(Exception('second call!'))
     with self.assertRaisesRegexp(Exception, 'second call'):
         stub('A')
     self.assertEqual(stub('A'), None)
     self.assertEqual(stub(), None)
コード例 #9
0
 def test400_correct_precedence(self):
     stub = SinonStub()
     stub.withArgs('A').returns('Arg of A')
     stub.onFirstCall().returns('First call!')
     stub.onSecondCall().returns('Second call!')
     stub.returns('No args')
     self.assertEqual(stub(), 'First call!')
     self.assertEqual(stub('A'), 'Arg of A')
     self.assertEqual(stub(), 'No args')
コード例 #10
0
 def test401_correct_precedence_throws(self):
     stub = SinonStub()
     stub.withArgs('A').throws(Exception('A'))
     stub.onFirstCall().throws(Exception('First call!'))
     stub.onSecondCall().throws(Exception('Second call!'))
     stub.throws(Exception('No args'))
     with self.assertRaisesRegexp(Exception, 'First call!'):
         stub()
     with self.assertRaisesRegexp(Exception, 'A'):
         stub('A')
     with self.assertRaisesRegexp(Exception, 'No args'):
         stub()
コード例 #11
0
 def test225_onCall_plus_withArgs(self):
     fto = ForTestOnly()
     self.assertEqual(fto.func1(), "func1")
     stub = SinonStub(ForTestOnly, "func1")
     stub.withArgs(1).returns("1")
     stub.withArgs(1).onSecondCall().returns("oncall")
     stub.onThirdCall().returns("##")
     stub.onCall(4).returns("###")  # on fifth call will return "###"
     self.assertEqual(fto.func1(1), "1")
     self.assertEqual(fto.func1(1), "oncall")
     self.assertEqual(fto.func1(1),
                      "1")  # the priority of onCall is lower than withArgs
     self.assertEqual(fto.func1(), None)
     self.assertEqual(fto.func1(), "###")
コード例 #12
0
ファイル: TestSinonMatcher.py プロジェクト: note35/sinon
 def test060_and_match(self):
     spy = SinonSpy()
     stub = SinonStub()
     m = SinonMatcher.instanceOf(spy).and_match(
         SinonMatcher.instanceOf(stub))
     self.assertFalse(m.mtest(spy))
     self.assertTrue(m.mtest(stub))
コード例 #13
0
 def test371_multiple_onCall_returns_named_functions(self):
     o = A_object()
     stub = SinonStub(o, 'A_func')
     stub.onFirstCall().returns(5)
     stub.onSecondCall().returns(10)
     stub.onThirdCall().returns(20)
     self.assertEqual(o.A_func(), 5)
     self.assertEqual(o.A_func(), 10)
     self.assertEqual(o.A_func(), 20)
コード例 #14
0
 def test220_returns(self):
     fto = ForTestOnly()
     self.assertEqual(fto.func1(), "func1")
     stub = SinonStub(ForTestOnly, "func1")
     stub.returns(1)
     self.assertEqual(fto.func1(), 1)
     stub.returns({})
     self.assertEqual(fto.func1(), {})
     stub.returns(TestSinonStub.my_func)
     self.assertEqual(fto.func1(), TestSinonStub.my_func)
コード例 #15
0
 def test222_withArgs(self):
     fto = ForTestOnly()
     stub = SinonStub(ForTestOnly, "func1")
     stub.withArgs(1).returns("#")
     self.assertEqual(fto.func1(1), "#")
     stub.withArgs(b=1).returns("##")
     self.assertEqual(fto.func1(b=1), "##")
     stub.withArgs(1, b=1).returns("###")
     self.assertEqual(fto.func1(1, b=1), "###")
     self.assertEqual(fto.func1(2), None)
コード例 #16
0
 def test425_returns_throws_can_be_overwritten(self):
     stub = SinonStub()
     self.assertEqual(stub(), None)
     stub.returns(5)
     self.assertEqual(stub(), 5)
     stub.throws()
     with self.assertRaises(Exception):
         stub()
     stub.returns(10)
     self.assertEqual(stub(), 10)
コード例 #17
0
 def test520_stub_is_SinonStubCondition_function(self):
     function_stub = SinonStub(C_func).onFirstCall().throws(
         BaseException('Hello World')).onSecondCall().returns(42)
     self.assertEqual(function_stub.callCount, 0)
     with self.assertRaisesRegexp(BaseException, 'Hello World'):
         sinon.g.C_func()
     self.assertEqual(sinon.g.C_func(), 42)
     self.assertEqual(sinon.g.C_func(), None)
     self.assertEqual(function_stub.callCount, 3)
コード例 #18
0
 def test510_stub_is_SinonStubCondition_module_function(self):
     module_function_stub = SinonStub(os, "system").onFirstCall().throws(
         BaseException('Hello World')).onSecondCall().returns(42)
     self.assertEqual(module_function_stub.callCount, 0)
     with self.assertRaisesRegexp(BaseException, 'Hello World'):
         os.system("cd")
     self.assertEqual(os.system("cd"), 42)
     self.assertEqual(os.system("cd"), None)
     self.assertEqual(module_function_stub.callCount, 3)
コード例 #19
0
 def test500_stub_is_SinonStubCondition_pure(self):
     pure_stub = SinonStub().onFirstCall().throws(
         BaseException('Hello World')).onSecondCall().returns(42)
     self.assertEqual(pure_stub.callCount, 0)
     with self.assertRaisesRegexp(BaseException, 'Hello World'):
         pure_stub()
     self.assertEqual(pure_stub(), 42)
     self.assertEqual(pure_stub(), None)
     self.assertEqual(pure_stub.callCount, 3)
コード例 #20
0
 def test370_multiple_onCall_returns(self):
     o = A_object()
     stub = SinonStub(o, 'A_func')
     stub.onCall(0).returns(5)
     stub.onCall(1).returns(10)
     stub.onCall(2).returns(20)
     stub.onCall(3).returns(30)
     self.assertEqual(o.A_func(), 5)
     self.assertEqual(o.A_func(), 10)
     self.assertEqual(o.A_func(), 20)
     self.assertEqual(o.A_func(), 30)
コード例 #21
0
 def test430_withArgs_onCall_combine_correctly_returns(self):
     stub = SinonStub()
     stub.withArgs('A').onFirstCall().returns(1)
     stub.withArgs('A').onSecondCall().returns(2)
     stub.withArgs('B').onFirstCall().returns(10)
     stub.withArgs('B').onSecondCall().returns(20)
     self.assertEqual(stub(), None)
     self.assertEqual(stub('A'), 1)
     self.assertEqual(stub('A'), 2)
     self.assertEqual(stub('A'), None)
     self.assertEqual(stub('B'), 10)
     self.assertEqual(stub('B'), 20)
     self.assertEqual(stub('B'), None)
コード例 #22
0
 def test432_withArgs_onCall_combine_correctly_class(self):
     fto = ForTestOnly()
     stub = SinonStub(ForTestOnly, "func1")
     stub.withArgs('A').onFirstCall().returns(1)
     stub.withArgs('A').onSecondCall().returns(2)
     stub.withArgs('B').onFirstCall().returns(10)
     stub.withArgs('B').onSecondCall().returns(20)
     self.assertEqual(stub(fto), None)
     self.assertEqual(stub(fto, 'A'), 1)
     self.assertEqual(stub(fto, 'A'), 2)
     self.assertEqual(stub(fto, 'A'), None)
     self.assertEqual(stub(fto, 'B'), 10)
     self.assertEqual(stub(fto, 'B'), 20)
     self.assertEqual(stub(fto, 'B'), None)
コード例 #23
0
 def test404_correct_precedence_withArgs_args_kwargs(self):
     stub = SinonStub()
     stub.withArgs('A', b='B').onSecondCall().returns('second call!')
     stub.withArgs('A', b='B').returns('Arg of A, Kwarg of B')
     self.assertEqual(stub(), None)
     self.assertEqual(stub('A', b='B'), 'Arg of A, Kwarg of B')
     self.assertEqual(stub('A', b='B'), 'second call!')
     self.assertEqual(stub('A', b='B'), 'Arg of A, Kwarg of B')
コード例 #24
0
 def test403_correct_precedence_withArgs_kwargs(self):
     stub = SinonStub()
     stub.withArgs(a='A').onSecondCall().returns('second call!')
     stub.withArgs(a='A').returns('Kwarg of A')
     self.assertEqual(stub(), None)
     self.assertEqual(stub(a='A'), 'Kwarg of A')
     self.assertEqual(stub(a='A'), 'second call!')
     self.assertEqual(stub(a='A'), 'Kwarg of A')
コード例 #25
0
 def test441_throws_does_not_reset_call_count(self):
     stub = SinonStub()
     stub.onFirstCall().throws(Exception('first call'))
     with self.assertRaisesRegexp(Exception, 'first call'):
         stub()
     stub.onSecondCall().throws(Exception('second call'))
     with self.assertRaisesRegexp(Exception, 'second call'):
         stub()
コード例 #26
0
 def test431_withArgs_onCall_combine_correctly_throws(self):
     stub = SinonStub()
     stub.withArgs('A').onFirstCall().throws(Exception('1'))
     stub.withArgs('A').onSecondCall().throws(Exception('2'))
     stub.withArgs('B').onFirstCall().throws(Exception('10'))
     stub.withArgs('B').onSecondCall().throws(Exception('20'))
     self.assertEqual(stub(), None)
     with self.assertRaisesRegexp(Exception, '1'):
         stub('A')
     with self.assertRaisesRegexp(Exception, '2'):
         stub('A')
     self.assertEqual(stub('A'), None)
     with self.assertRaisesRegexp(Exception, '10'):
         stub('B')
     with self.assertRaisesRegexp(Exception, '20'):
         stub('B')
     self.assertEqual(stub('B'), None)
コード例 #27
0
ファイル: TestSinonAssertion.py プロジェクト: note35/sinon
 def test043_callOrder_three_unique_args_call_repeated(self):
     spy1 = SinonSpy()
     stub1 = SinonStub()
     spy2 = SinonSpy()
     spy1()
     stub1()
     spy2()
     spy1()
     SinonAssertion.callOrder(spy1, stub1, spy2)
     SinonAssertion.callOrder(spy1, stub1, spy2, spy1)
     SinonAssertion.callOrder(stub1, spy2, spy1)
     SinonAssertion.callOrder(stub1, spy1)
     SinonAssertion.callOrder(spy1, stub1)
     SinonAssertion.callOrder(spy2, spy1)
     SinonAssertion.callOrder(spy1, spy2)
コード例 #28
0
 def test223_onCall(self):
     fto = ForTestOnly()
     self.assertEqual(fto.func1(), "func1")
     stub = SinonStub(ForTestOnly, "func1")
     stub.onSecondCall().returns("oncall")
     self.assertEqual(fto.func1(), None)
     self.assertEqual(fto.func1(), "oncall")
     stub.onThirdCall().returns("oncall")
     self.assertEqual(fto.func1(), "oncall")
     self.assertEqual(fto.func1(), None)
コード例 #29
0
 def test221_throws(self):
     fto = ForTestOnly()
     self.assertEqual(fto.func1(), "func1")
     stub = SinonStub(ForTestOnly, "func1")
     stub.throws()
     with self.assertRaises(Exception) as context:
         fto.func1()
     stub.throws(TypeError)
     with self.assertRaises(TypeError) as context:
         fto.func1()
コード例 #30
0
 def test408_chained_withArgs_onCall_returns(self):
     stub = SinonStub()
     stub.withArgs(42).onFirstCall().returns(1).onSecondCall().returns(2)
     stub.returns(0)
     self.assertEqual(stub(1), 0)
     self.assertEqual(stub(42), 1)
     self.assertEqual(stub(1), 0)
     self.assertEqual(stub(42), 2)
     self.assertEqual(stub(1), 0)
     self.assertEqual(stub(42), 0)