# -*- coding: utf-8 -*- from mockstar import BaseTestCase from mockstar import prefixed_p from app.bl.user import create_user ppatch = prefixed_p('app.bl.user', autospec=True) class TestCreateUser(BaseTestCase): @ppatch('discover_possible_friends') @ppatch('mail') @ppatch('choose_high_quality_avatar') @ppatch('choose_low_quality_avatar') @ppatch('count_score') @ppatch('not_md5_and_has_salt') @ppatch('User') def side_effects(self, se): se.user = se.User.return_value se.secure_pwd = se.not_md5_and_has_salt.return_value se.score = se.count_score.return_value return self.invoke(se) def test_should_create_save_and_return_user(self, se): # do rv = create_user("*****@*****.**", "pwd", "Foo Bar") se.User.assert_called_with( email="*****@*****.**", password=se.secure_pwd, full_name="Foo Bar")
self.assertIsInstance(se.side_effect_two, MagicMock) class TestM(TestCase): def test_should_create_object(self): m = M() m.asd = 'dsa' self.assertEquals(m.asd, 'dsa') def test_should_make_couple_rv(self): m = M() m.foo.rv.bar.rv.baz.rv = 28 self.assertEquals(m.foo().bar().baz(), 28) ppatch = prefixed_p(__name__) # ppatch_autospec_M = prefixed_p(__name__, autospec=True, to_m=True) ppatch_autospec = prefixed_p(__name__, autospec=False) class TestPrefixedP(TestCase): @ppatch('side_effect_five', autospec=True) def test_should_prefix_patch_here(self, se): self.assertRaises(TypeError, lambda: se.side_effect_five()) @ppatch_autospec('side_effect_five', autospec=True) def test_should_rewrite_ppatch_param(self, se): self.assertRaises(TypeError, lambda: se.side_effect_five()) # @ppatch_autospec_M('side_effect_five') # def test_should_return_m_and_raise_typeerror(self, se):
import unittest from mockstar import prefixed_p from sample_app.blog import forms ppatch = prefixed_p("sample_app.blog.forms") # module under test class TestPostForm(unittest.TestCase): @ppatch('is_post_exist') # list / describe side-effects def side_effects(self, se): se.is_post_exist.return_value = False # default side-effects behavior return self.invoke(se) def test_should_be_valid_for_simple_data(self, se): form = forms.PostForm({'title': 'foo', 'content': 'bar'}) self.assertTrue(form.is_valid()) def test_should_get_error_on_existing_post_title(self, se): se.is_post_exist.return_value = True form = forms.PostForm({'title': 'foo', 'content': 'bar'}) self.assertFalse(form.is_valid()) self.assertEquals(dict(form.errors), ["Post with this title already exists"])