예제 #1
0
    def test_context_manager_interface_when_unstubbed(self):
        m = Mock()

        def cm():
            with m:
                pass

        self.assertRaises(UnexpectedCall, cm)
    def test_stub_mock_with_obj_ref(self):
        class Foo(object):
            def bar(self):
                pass

        f = Foo()
        f.bar = Mock()
        res = stub(f.bar)
        self.assertTrue(isinstance(res, StubMethod))
        self.assertEquals(res, f.bar.__call__)
        self.assertEquals(res, stub(f.bar))
예제 #3
0
    def test_stub_mock_with_attr_name(self):
        class Foo(object):
            def bar(self):
                pass

        f = Foo()
        f.bar = Mock()
        res = stub(f, 'bar')
        self.assertTrue(isinstance(res, StubMethod))
        self.assertEqual(res, f.bar.__call__)
        self.assertEqual(res, stub(f, 'bar'))
예제 #4
0
 def test_container_interface_when_unstubbed(self):
     m = Mock()
     self.assertRaises(UnexpectedCall, len, m)
     self.assertRaises(UnexpectedCall, m.__getitem__, 'foo')
     self.assertRaises(UnexpectedCall, m.__setitem__, 'foo', 'bar')
     self.assertRaises(UnexpectedCall, m.__delitem__, 'foo')
     self.assertRaises(UnexpectedCall, iter, m)
     self.assertRaises(UnexpectedCall, reversed, m)
     self.assertRaises(UnexpectedCall, m.__contains__, 'foo')
     self.assertRaises(UnexpectedCall, m.__getslice__, 'i', 'j')
     self.assertRaises(UnexpectedCall, m.__setslice__, 'i', 'j', 'foo')
     self.assertRaises(UnexpectedCall, m.__delslice__, 'i', 'j')
예제 #5
0
    def test_context_manager_interface_when_stubbed(self):
        m = Mock()
        exc = Exception('fail')

        def cm_noraise():
            with m:
                pass

        def cm_raise():
            with m:
                raise exc

        stub(m.__enter__).expect().times(2)
        stub(m.__exit__).expect().args(None, None, None)
        stub(m.__exit__).expect().args(Is(Exception), exc, types.TracebackType)

        cm_noraise()
        self.assertRaises(Exception, cm_raise)
예제 #6
0
    def test_container_interface_when_stubbed(self):
        m = Mock()
        i = iter([1, 2, 3])

        stub(m.__len__).expect().returns(42)
        stub(m.__getitem__).expect().args('foo').returns('getitem')
        stub(m.__setitem__).expect().args('foo', 'bar')
        stub(m.__delitem__).expect().args('foo')
        stub(m.__iter__).expect().returns(i)
        stub(m.__reversed__).expect().returns('backwards')
        stub(m.__contains__).expect().args('foo').returns(True)

        self.assertEqual(42, len(m))
        self.assertEqual('getitem', m['foo'])
        m['foo'] = 'bar'
        del m['foo']
        self.assertEqual(i, iter(m))
        self.assertEqual('backwards', reversed(m))
        self.assertEqual(True, 'foo' in m)
예제 #7
0
파일: mock_test.py 프로젝트: Sushant/chai
 def test_get_attribute_auto_method_not_raises_unexpectedcall_when_stubbed(self):
   m = Mock()
   stub(m.foo).expect().returns('success')
   self.assertEquals( 'success', m.foo() )
예제 #8
0
 def test_get_attribute_supports_multiple_depths(self):
     m = Mock()
     self.assertTrue(isinstance(m.foo.bar, Mock))
예제 #9
0
 def test_nonzero_returns_true_when_unstubbed(self):
     m = Mock()
     self.assertTrue(m.__nonzero__())
예제 #10
0
 def test_nonzero_when_stubbed(self):
     m = Mock()
     stub(m.__nonzero__).expect().returns(False)
     self.assertFalse(m.__nonzero__())
예제 #11
0
 def test_call_raises_unexpectedcall_when_unstubbed(self):
     m = Mock()
     self.assertRaises(UnexpectedCall, m)
예제 #12
0
 def test_call_not_raises_unexpectedcall_when_stubbed(self):
     m = Mock()
     stub(m).expect().returns('success')
     self.assertEqual('success', m())
예제 #13
0
 def test_get_attribute_auto_method_not_raises_unexpectedcall_multiple_depths(
         self):
     m = Mock()
     stub(m.foo.bar).expect().returns('success')
     self.assertEqual('success', m.foo.bar())
예제 #14
0
 def test_get_attribute_does_not_overwrite_existing_attr(self):
     m = Mock()
     m.foo = 42
     self.assertEqual(42, m.foo)
예제 #15
0
 def test_get_attribute_auto_method_raises_unexpectedcall_when_stubbed(
         self):
     m = Mock()
     stub(m.foo)
     self.assertRaises(UnexpectedCall, m.foo)
예제 #16
0
 def test_get_attribute_auto_method_not_raises_unexpectedcall_when_stubbed(
         self):
     m = Mock()
     stub(m.foo).expect().returns('success')
     self.assertEqual('success', m.foo())
예제 #17
0
파일: mock_test.py 프로젝트: Sushant/chai
 def test_get_attribute_does_not_overwrite_existing_attr(self):
   m = Mock()
   m.foo = 42
   self.assertEquals( 42, m.foo )
예제 #18
0
 def test_get_attribute_caches_auto_methods(self):
     m = Mock()
     self.assertTrue(m.foo is m.foo)
예제 #19
0
 def test_get_attribute_creates_a_mock_method(self):
     m = Mock()
     self.assertEqual('mock', m._name)
     self.assertTrue(isinstance(m.foo, Mock))
     self.assertFalse(m.foo is m)
     self.assertEqual('mock.foo', m.foo._name)
예제 #20
0
파일: mock_test.py 프로젝트: Sushant/chai
 def test_nonzero_returns_true_when_unstubbed(self):
   m = Mock()
   self.assertTrue( m.__nonzero__() )
예제 #21
0
파일: mock_test.py 프로젝트: Sushant/chai
 def test_nonzero_when_stubbed(self):
   m = Mock()
   stub(m.__nonzero__).expect().returns(False)
   self.assertFalse( m.__nonzero__() )