def test_fetch_news_items_raise_FeedReaderException(self): # TODO: create a mock RssNewsFeedParser object and assign it to a local # variable named `mock_news_feed_parser` mock_news_feed_parser = Mock(spec=RssNewsFeedParser) # TODO: configure the mock so that a call to its get_news() method has # the side effect of raising a FeedReaderException. mock_news_feed_parser.get_news.side_effect = FeedReaderException() # TODO: create a FeedReader instance and assign it to a local variable # named `feed_reader` feed_reader = FeedReader() # TODO: set the feed_reader.news_feed_parser attribute to # mock_news_feed_parser feed_reader.news_feed_parser = mock_news_feed_parser # TODO: call the feed_reader's fetch_news_items() method and save the # return value in a variable named `news`. (Pass any string as the # argument to fetch_news_items()) news = feed_reader.fetch_news_items('quack') # TODO: assert that the `news` variable is an instance of list. self.assertTrue(isinstance(news, list)) # TODO: assert that the length of the `news` list is 0 self.assertEqual(0, len(news))
def test_fetch_news_items_music(self): # TODO: create a mock RssNewsFeedParser object and assign it to a local # variable named `mock_news_feed_parser` mock_news_feed_parser = Mock(spec=RssNewsFeedParser) # TODO: set the return value of the mock's get_news() method to the # value of the variable `expected` (defined at the end of the file) mock_news_feed_parser.get_news.return_value = expected # TODO: create a FeedReader instance and assign it to a local variable # named `feed_reader` feed_reader = FeedReader() # TODO: set the feed_reader.news_feed_parser attribute to the # mock_news_feed_parser feed_reader.news_feed_parser = mock_news_feed_parser # TODO: note the call the call to feed_reader.fetch_news_items(). # Because you changed the feed reader's `news_feed_parser` attribute in # the previous statement, the feed_reader will get news from the mock # object instead of a RssNewsFeedParser instance. # (no code change required) news = feed_reader.fetch_news_items("music") # TODO: note that we verify the result as usual. # (no code change required) for expected_result, actual_result in zip_longest(expected, news): self.assertEqual(expected_result, actual_result)
def test_fetch_news_items_patch_decorator(self, mock_news_feed_parser_class): # Always patch an object where it is imported, not where it is defined. # Here, we're testing a method in # ticketmanor.rest_services.feed_reader.feed_reader.FeedReader, which # imports RssNewsFeedParser. So to mock RssNewsFeedParser, patch() # needs to know the package that is importing RssNewsFeedParser, not # the package that defines RssNewsFeedParser. # Note that the second argument to the test method is a # Mock class, not a Mock instance, so we need an additional step # to get a Mock instance for setting the get_news() return value. mock_news_feed_parser = mock_news_feed_parser_class.return_value mock_news_feed_parser.get_news.return_value = expected feed_reader = FeedReader() news = feed_reader.fetch_news_items("music", 2) # verify that the mock's get_news() method was called correctly # (for details, see Appendix B) mock_news_feed_parser.get_news.assert_called_once_with("music", 2) # verify results for expected_result, actual_result in zip_longest(expected, news): self.assertEqual(expected_result, actual_result)
def test_fetch_news_items_max_items_2(self): # test set up mock_news_feed_parser = Mock(spec=RssNewsFeedParser) mock_news_feed_parser.get_news.return_value = expected feed_reader = FeedReader() feed_reader.news_feed_parser = mock_news_feed_parser # call method under test news = feed_reader.fetch_news_items("music", 2) # verify that the mock's get_news() method was called correctly # (for details, see Appendix B) mock_news_feed_parser.get_news.assert_called_once_with("music", 2) # verify results for expected_result, actual_result in zip_longest(expected, news): self.assertEqual(expected_result, actual_result)
def test_fetch_news_items_patch_object_decorator(self, mock_get_news_method): # Set the return value of the mocked method to the `expected` # variable (defined at the end of the file) mock_get_news_method.return_value = expected # Note the call to the FeedReader constructor. The call to the # RssNewsFeedParser will happen as usual, but because we added # @patch.object to this test method, the RssNewsFeedParser's get_news # attribute will be replaced by a Mock. feed_reader = FeedReader() # Note that the call to the feed_reader's fetch_news_items() # will call the Mock's get_news(), which you programmed to return # `expected` news = feed_reader.fetch_news_items("music", 1) # verify that the mock's get_news() method was called correctly # (for details, see Appendix B) mock_get_news_method.assert_called_once_with("music", 1) # verify results for expected_result, actual_result in zip_longest(expected, news): self.assertEqual(expected_result, actual_result)
def setUp(self): # The arg to FeedReader.__init__() is the class of the news feed # parser. FeedReader will call the constructor of the news feed parser # class, so we'll let it create a Mock, then we can access the Mock as # an attribute of the feed parser. self.feed_reader = FeedReader(news_feed_parser_class=Mock)
def __init__(self): self.feed_reader = FeedReader()