当前位置: 首页>>代码示例>>Python>>正文


Python MagicMock.side_effect方法代码示例

本文整理汇总了Python中tests.functional.MagicMock.side_effect方法的典型用法代码示例。如果您正苦于以下问题:Python MagicMock.side_effect方法的具体用法?Python MagicMock.side_effect怎么用?Python MagicMock.side_effect使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在tests.functional.MagicMock的用法示例。


在下文中一共展示了MagicMock.side_effect方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_get_song

# 需要导入模块: from tests.functional import MagicMock [as 别名]
# 或者: from tests.functional.MagicMock import side_effect [as 别名]
    def test_get_song(self, MockedBeautifulSoup, mocked_build_opener):
        """
        Test that the get_song() method returns the correct song details
        """
        # GIVEN: A bunch of mocked out stuff and an importer object
        mocked_song_page = MagicMock()
        mocked_copyright = MagicMock()
        mocked_copyright.find_all.return_value = [MagicMock(string='Copyright 1'), MagicMock(string='Copyright 2')]
        mocked_song_page.find.side_effect = [
            mocked_copyright,
            MagicMock(find=MagicMock(string='CCLI: 123456'))
        ]
        mocked_lyrics_page = MagicMock()
        mocked_find_all = MagicMock()
        mocked_find_all.side_effect = [
            [
                MagicMock(contents='The Lord told Noah: there\'s gonna be a floody, floody'),
                MagicMock(contents='So, rise and shine, and give God the glory, glory'),
                MagicMock(contents='The Lord told Noah to build him an arky, arky')
            ],
            [MagicMock(string='Verse 1'), MagicMock(string='Chorus'), MagicMock(string='Verse 2')]
        ]
        mocked_lyrics_page.find.return_value = MagicMock(find_all=mocked_find_all)
        MockedBeautifulSoup.side_effect = [mocked_song_page, mocked_lyrics_page]
        mocked_callback = MagicMock()
        importer = SongSelectImport(None)
        fake_song = {'title': 'Title', 'authors': ['Author 1', 'Author 2'], 'link': 'url'}

        # WHEN: get_song is called
        result = importer.get_song(fake_song, callback=mocked_callback)

        # THEN: The callback should have been called three times and the song should be returned
        self.assertEqual(3, mocked_callback.call_count, 'The callback should have been called twice')
        self.assertIsNotNone(result, 'The get_song() method should have returned a song dictionary')
        self.assertEqual(2, mocked_lyrics_page.find.call_count, 'The find() method should have been called twice')
        self.assertEqual(2, mocked_find_all.call_count, 'The find_all() method should have been called twice')
        self.assertEqual([call('div', 'song-viewer lyrics'), call('div', 'song-viewer lyrics')],
                         mocked_lyrics_page.find.call_args_list,
                         'The find() method should have been called with the right arguments')
        self.assertEqual([call('p'), call('h3')], mocked_find_all.call_args_list,
                         'The find_all() method should have been called with the right arguments')
        self.assertIn('copyright', result, 'The returned song should have a copyright')
        self.assertIn('ccli_number', result, 'The returned song should have a CCLI number')
        self.assertIn('verses', result, 'The returned song should have verses')
        self.assertEqual(3, len(result['verses']), 'Three verses should have been returned')
开发者ID:imkernel,项目名称:openlp,代码行数:47,代码来源:test_songselect.py


注:本文中的tests.functional.MagicMock.side_effect方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。