當前位置: 首頁>>代碼示例>>Python>>正文


Python mock.fish方法代碼示例

本文整理匯總了Python中unittest.mock.fish方法的典型用法代碼示例。如果您正苦於以下問題:Python mock.fish方法的具體用法?Python mock.fish怎麽用?Python mock.fish使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在unittest.mock的用法示例。


在下文中一共展示了mock.fish方法的6個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: test_side_effect

# 需要導入模塊: from unittest import mock [as 別名]
# 或者: from unittest.mock import fish [as 別名]
def test_side_effect(self):
        mock = Mock()

        def effect(*args, **kwargs):
            raise SystemError('kablooie')

        mock.side_effect = effect
        self.assertRaises(SystemError, mock, 1, 2, fish=3)
        mock.assert_called_with(1, 2, fish=3)

        results = [1, 2, 3]
        def effect():
            return results.pop()
        mock.side_effect = effect

        self.assertEqual([mock(), mock(), mock()], [3, 2, 1],
                          "side effect not used correctly")

        mock = Mock(side_effect=sentinel.SideEffect)
        self.assertEqual(mock.side_effect, sentinel.SideEffect,
                          "side effect in constructor not used")

        def side_effect():
            return DEFAULT
        mock = Mock(side_effect=side_effect, return_value=sentinel.RETURN)
        self.assertEqual(mock(), sentinel.RETURN) 
開發者ID:war-and-code,項目名稱:jawfish,代碼行數:28,代碼來源:testmock.py

示例2: test_java_exception_side_effect

# 需要導入模塊: from unittest import mock [as 別名]
# 或者: from unittest.mock import fish [as 別名]
def test_java_exception_side_effect(self):
        import java
        mock = Mock(side_effect=java.lang.RuntimeException("Boom!"))

        # can't use assertRaises with java exceptions
        try:
            mock(1, 2, fish=3)
        except java.lang.RuntimeException:
            pass
        else:
            self.fail('java exception not raised')
        mock.assert_called_with(1,2, fish=3) 
開發者ID:war-and-code,項目名稱:jawfish,代碼行數:14,代碼來源:testmock.py

示例3: test_assert_called_with

# 需要導入模塊: from unittest import mock [as 別名]
# 或者: from unittest.mock import fish [as 別名]
def test_assert_called_with(self):
        mock = Mock()
        mock()

        # Will raise an exception if it fails
        mock.assert_called_with()
        self.assertRaises(AssertionError, mock.assert_called_with, 1)

        mock.reset_mock()
        self.assertRaises(AssertionError, mock.assert_called_with)

        mock(1, 2, 3, a='fish', b='nothing')
        mock.assert_called_with(1, 2, 3, a='fish', b='nothing') 
開發者ID:war-and-code,項目名稱:jawfish,代碼行數:15,代碼來源:testmock.py

示例4: test_method_calls_recorded

# 需要導入模塊: from unittest import mock [as 別名]
# 或者: from unittest.mock import fish [as 別名]
def test_method_calls_recorded(self):
        mock = Mock()
        mock.something(3, fish=None)
        mock.something_else.something(6, cake=sentinel.Cake)

        self.assertEqual(mock.something_else.method_calls,
                          [("something", (6,), {'cake': sentinel.Cake})],
                          "method calls not recorded correctly")
        self.assertEqual(mock.method_calls, [
            ("something", (3,), {'fish': None}),
            ("something_else.something", (6,), {'cake': sentinel.Cake})
        ],
            "method calls not recorded correctly") 
開發者ID:war-and-code,項目名稱:jawfish,代碼行數:15,代碼來源:testmock.py

示例5: test_wraps_calls

# 需要導入模塊: from unittest import mock [as 別名]
# 或者: from unittest.mock import fish [as 別名]
def test_wraps_calls(self):
        real = Mock()

        mock = Mock(wraps=real)
        self.assertEqual(mock(), real())

        real.reset_mock()

        mock(1, 2, fish=3)
        real.assert_called_with(1, 2, fish=3) 
開發者ID:war-and-code,項目名稱:jawfish,代碼行數:12,代碼來源:testmock.py

示例6: test_assert_has_calls

# 需要導入模塊: from unittest import mock [as 別名]
# 或者: from unittest.mock import fish [as 別名]
def test_assert_has_calls(self):
        kalls1 = [
                call(1, 2), ({'a': 3},),
                ((3, 4),), call(b=6),
                ('', (1,), {'b': 6}),
        ]
        kalls2 = [call.foo(), call.bar(1)]
        kalls2.extend(call.spam().baz(a=3).call_list())
        kalls2.extend(call.bam(set(), foo={}).fish([1]).call_list())

        mocks = []
        for mock in Mock(), MagicMock():
            mock(1, 2)
            mock(a=3)
            mock(3, 4)
            mock(b=6)
            mock(1, b=6)
            mocks.append((mock, kalls1))

        mock = Mock()
        mock.foo()
        mock.bar(1)
        mock.spam().baz(a=3)
        mock.bam(set(), foo={}).fish([1])
        mocks.append((mock, kalls2))

        for mock, kalls in mocks:
            for i in range(len(kalls)):
                for step in 1, 2, 3:
                    these = kalls[i:i+step]
                    mock.assert_has_calls(these)

                    if len(these) > 1:
                        self.assertRaises(
                            AssertionError,
                            mock.assert_has_calls,
                            list(reversed(these))
                        ) 
開發者ID:war-and-code,項目名稱:jawfish,代碼行數:40,代碼來源:testmock.py


注:本文中的unittest.mock.fish方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。