PYCON 88
Mock Guest on 5th May 2020 04:05:13 AM
  1. from mock import MagicMock
  2. thing = ProductionClass()
  3. thing.method = MagicMock(return_value=3)
  4. thing.method(3, 4, 5, key='value')
  5.  
  6. thing.method.assert_called_with(3, 4, 5, key='value')
  7.     class MockSearchQuerySet(SearchQuerySet):
  8.         def __iter__(self):
  9.             return iter(["foo", "bar", "baz"])
  10.     return MockSearchQuerySet()
  11.  
  12. # SearchForm here refers to the imported class reference in myapp,
  13. # not where the SearchForm class itself is imported from
  14. @mock.patch('myapp.SearchForm.search', mock_search)
  15. def test_new_watchlist_activities(self):
  16.     # get_search_results runs a search and iterates over the result
  17.     self.assertEqual(len(myapp.get_search_results(q="fish")), 3)

Paste is for source code and general debugging text.

Login or Register to edit, delete and keep track of your pastes and more.

Raw Paste

Login or Register to edit or fork this paste. It's free.