def test_unstub_returns_back_original_method_even_in_multiple_stubs(self): other_value = object() stubydoo.stub(self.object.method) stubydoo.stub(self.object.method).with_args('arg', 1, foo='bar').\ and_return(other_value) stubydoo.unstub(self.object.method) self.assertTrue(self.object.method() is self.original_value)
def setUp(self): placelesssetup.setUp(self) self.mailhost = stubydoo.double() class IPossibleSubscriberProvider(zope.interface.Interface): pass self.subscriber = stubydoo.double() self.subscribers = [self.subscriber] self.newsletter = stubydoo.double() self.message = stubydoo.double() stubydoo.stub(self.newsletter, 'compile').with_args(self.subscriber).\ and_return(self.message) stubydoo.stub(self.mailhost, 'send').with_args(self.message) @zope.component.adapter(None) @zope.interface.implementer(interfaces.INewsletter) def newsletter_adapter(context): self.newsletter.context = context return self.newsletter zope.component.provideAdapter(newsletter_adapter) self.context = stubydoo.double() self.mailing = mailing.Mailing()
def test_traversing_to_a_unknown_key_not_convertible_to_url_raises_not_found(self): cache_keys = zope.component.getUtility(interfaces.ICacheKeys) stub(cache_keys.getURL).with_args('unknown key').and_return(None) self.assertRaises( zope.publisher.interfaces.NotFound, self.cache_traverser.publishTraverse, 'request', 'unknown key' )
def test_unset_in_specific_keeps_generic_stub(self): value = object() other_value = object() stubydoo.stub(self.object.method).and_return(value) specific = stubydoo.stub(self.object.method).\ with_args('arg', 1, foo='bar').and_return(other_value) specific.unset() self.assertTrue(self.object.method('any args') is value)
def test_doesnt_raise_error_if_no_key_is_left_and_cache_has_no_key(self): context = object() stub(self.context_keys.__contains__).with_args('the key').\ and_return(False) try: self.cache_manager.remove('the key', context) except: self.fail('Removal of key failed unexpectedly')
def test_removes_value_from_cache_if_no_key_is_left(self): context = object() self.cache['the key'] = object() stub(self.context_keys.__contains__).with_args('the key').\ and_return(False) self.cache_manager.remove('the key', context) self.assertTrue('the key' not in self.cache)
def test_keeps_value_in_cache_if_theres_a_link(self): context = object() self.cache['the key'] = object() stub(self.context_keys.__contains__).with_args('the key').\ and_return(True) self.cache_manager.remove('the key', context) self.assertTrue('the key' in self.cache)
def test_keeps_values_not_associated_with_context_in_cache(self): context = object() keys_to_remove = ['the key', 'another key'] stub(self.context_keys.unlinkContext).with_args(context).\ and_return(keys_to_remove) self.cache_manager.removeAll(context) self.assertTrue('yet another key' in self.cache)
def test_add_returns_error_messages_from_form(self): def fn(): return (dict(email='email', format='format'), [stubydoo.double(error=RuntimeError('foo')), stubydoo.double(error=RuntimeError('bar'))]) stubydoo.stub(self.add_form.extractData).and_run(fn) errors = self.adder.add('email', 'format', None, None) self.assertEquals(errors, 'foo, bar')
def test_function_arguments(self): def fn(*args, **kw): return (args, kw) stubydoo.stub(self.double, 'method').\ with_args('a', 'b', a='a', b='b').and_run(fn) self.assertEquals(self.double.method('a', 'b', a='a', b='b'), (('a', 'b'), {'a': 'a', 'b': 'b'}))
def test_keyword_argument_matching(self): stubydoo.stub(self.double, 'method').and_return('not matched') stubydoo.stub(self.double, 'method').\ with_kwargs(self.dict_including(foo='bar', spam='eggs')).\ and_return('matched') returned_value = self.double.method( foo='bar', spam='eggs', some_other_key='some other value' ) self.assertEquals(returned_value, 'matched')
def test_compile_returns_message_from_factory(self): message = 'The resulting message' self.newsletter_attributes.html = u'The original HTML' stubydoo.stub(self.message_factory.__call__).\ with_args(string_containing(u'The original HTML')).\ and_return(message) self.assertEquals(self.newsletter.compile(self.subscriber), 'The resulting message')
def test_traversing_to_a_unknown_key_redirects(self): response = double(redirect=lambda self, url:None) request = double(response=response) expect(response.redirect).with_args('the original url') cache_keys = zope.component.getUtility(interfaces.ICacheKeys) stub(cache_keys.getURL).with_args('unknown key').\ and_return('the original url') self.cache_traverser.publishTraverse(request, 'unknown key')
def test_unstubbing(self): stubydoo.stub(self.double, 'method') stubydoo.unstub(self.double.method) try: self.double.method except AttributeError: pass else: self.fail()
def test_compile_returns_message_from_factory(self): message = 'The resulting message' self.newsletter_attributes.html = u'The original HTML' stubydoo.stub(self.message_factory.__call__).\ with_args(string_containing(u'The original HTML')).\ and_return(message) self.assertEquals(self.newsletter.compile(self.subscriber), 'The resulting message')
def test_add_adds_subscriber_with_form_data(self): obj = stubydoo.double() obj.id = 'the subscriber id' stubydoo.stub(self.add_form.extractData).\ and_return(('the data', None)) stubydoo.expect(self.add_form.createAndAdd).\ with_args('the data').and_return(obj) self.adder.add('email', 'format', None, None)
def test_compiles_template_with_context(self): compilation = double() stubydoo.stub(compilation, '__unicode__').and_return(u'Result') @zope.component.adapter(None) @zope.interface.implementer(interfaces.ICompilation) def compilation_adapter(context): return compilation zope.component.provideAdapter(compilation_adapter) self.assertEquals(self.view.render(), u'Result')
def test_fallback_when_there_is_no_match(self): stubydoo.stub(self.double, 'method').and_return('not matched') stubydoo.stub(self.double, 'method').\ with_args(self.dict_including(foo='bar')).\ with_kwargs(self.dict_including(foo='bar', spam='eggs')).\ and_return('matched') returned_value = self.double.method( dict(foo='bar', some_other_key='some other value'), some_other_key='some other value' ) self.assertEquals(returned_value, 'not matched')
def setUp(self): placelesssetup.setUp(self) self.context = zope.container.folder.Folder() self.request = stubydoo.double(form={}) self.add_form = AddForm(self.context) self.adder = subscriber_list.SubscriberAdder(self.context, self.request, self.add_form) dates_locale = stubydoo.double() self.request.locale = stubydoo.double(dates=dates_locale) self.parsed_datetime = datetime.datetime.now() self.date_short_fmt = stubydoo.double( parse=lambda f, x: self.parsed_datetime ) stubydoo.stub(dates_locale, 'getFormatter').\ with_args('date', 'short').and_return(self.date_short_fmt) stubydoo.stub(dates_locale, 'getFormatter').\ with_args('date', 'medium').\ and_return(stubydoo.double(parse='ignored')) stubydoo.stub(dates_locale, 'getFormatter').\ with_args('dateTime', 'short').\ and_return(stubydoo.double(parse='ignored')) stubydoo.stub(dates_locale, 'getFormatter').\ with_args('dateTime', 'medium').\ and_return(stubydoo.double(parse='ignored')) zope.component.provideAdapter(Publication)
def test_add_should_set_subscriber_deactivation_date(self): # Don't break if the subscriber is to be wrapped. obj = stubydoo.double(__of__=lambda self, container: self) obj.id = 'the subscriber id' stubydoo.stub(self.add_form.extractData).\ and_return(('the data', None)) stubydoo.stub(self.add_form.createAndAdd).\ with_args('the data').and_return(obj) self.adder.add('email', 'format', None, 'deactivation') self.assertEquals(subscriber.getSubscriberDeactivation(obj), self.parsed_datetime)
def setUp(self): placelesssetup.setUp(self) self.compilation_strategy = double() @zope.component.adapter(None, INullTemplateConfiguration) @zope.interface.implementer(interfaces.ICompilationStrategy) def compilation_strategy(content, config): self.compilation_strategy.context = content self.compilation_strategy.config = config return self.compilation_strategy zope.component.provideAdapter(compilation_strategy) stub(self.compilation_strategy, 'compile').\ and_return('Compilation results')
def test_with_exception_arguments(self): class MyError(Exception): def __init__(self, *args, **kwargs): self.args = args self.kwargs = kwargs self.error = MyError stubydoo.stub(self.double, 'method').\ and_raise(self.error, 'a1', 'a2', a3='a3', a4='a4') try: self.double.method() except MyError as exc: self.assertEquals(exc.args, ('a1', 'a2')) self.assertEquals(exc.kwargs, {'a3': 'a3', 'a4': 'a4'}) else: self.fail('Expected to have an error raised')
def test_keeps_links_if_cant_download(self): stub(url_opener, 'open').\ with_args('http://external.com/image.png').\ and_raise(urllib2.HTTPError, 'http://external.com/image.png', 666, 'an error occurred', {}, None) self.subject.update() replaced_html = lxml.html.fromstring(self.subject.context.original_html) self.assertEquals( lxml.html.tostring(lxml.html.fromstring(self.provided_html), pretty_print=True), lxml.html.tostring(replaced_html, pretty_print=True) )
def setUp(self): placelesssetup.setUp(self) self.context = double() self.templating_behavior = double() template_content = double() self.templating_behavior.template_object = template_content zope.interface.alsoProvides(template_content, IPossibleTemplate) @zope.component.adapter(None) @zope.interface.implementer(ITemplating) def templating_behavior(context): return self.templating_behavior zope.component.provideAdapter(templating_behavior) template = double() @zope.component.adapter(IPossibleTemplate) @zope.interface.implementer(interfaces.ITemplate) def template_adapter(context): return template zope.component.provideAdapter(template_adapter) stub(template, 'compile').with_args(self.context).and_return(u'result')
def setUp(self): placelesssetup.setUp(self) zope.component.provideUtility(cache_manager, interfaces.ICacheManager) zope.component.provideUtility(cache_keys, interfaces.ICacheKeys) zope.component.provideAdapter(Cacheable) setSite(portal) stub(url_opener, 'open').with_args('http://external.com/image.png').\ and_return(StringIO('the image contents')) stub(cache_keys.getKey).with_args('http://external.com/image.png').\ and_return('=the-cache-key') stub(cache_keys.getURL).with_args('=the-cache-key').\ and_return('http://external.com/image.png')
def test(): stubydoo.stub(self.double, 'method').\ with_args('a', 'b', a='a', b='b').and_yield(iterator) return [v for v in self.double.method('a', 'b', a='a', b='b')]
def test(): stubydoo.stub(self.double, 'method').and_return('a value')
def test(): stubydoo.stub(self.double, 'method') stubydoo.expect(self.double, 'method') self.double.method()
def test_methods_using_lambda_can_be_stubbed(self): double = stubydoo.double(method=lambda self: 'value') stubydoo.stub(double.method).and_return('another value') self.assertEquals(double.method(), 'another value')
def test_methods_using_lambda_can_be_stubbed(self): mock = stubydoo.mock(method=lambda self: 'value') stubydoo.stub(mock.method).and_return('another value') self.assertEquals(mock.method(), 'another value')
def test_methods_using_lambda_can_be_stubbed(self): null = stubydoo.null(method=lambda self: 'value') stubydoo.stub(null.method).and_return('another value') self.assertEquals(null.method(), 'another value')