Пример #1
0
		def failing_mock_expectation(slf):
			mocktest.mock().is_expected
			# emulate a refresh
			try:
				core._teardown()
			finally:
				core._setup()
Пример #2
0
	def test_should_hijack_setup_and_teardown(self):
		lines = []
		def capture(line):
			lines.append(line)
		backup_setup = core._setup
		backup_teardown = core._teardown
		
		core._setup = mock().with_action(lambda: capture("_setup")).raw
		core._teardown = mock().with_action(lambda: capture("_teardown")).raw
		
		class Foo(mocktest.TestCase):
			def setUp(self):
				capture("setup")
			def tearDown(self):
				capture("teardown")
			def test_main_is_called(self):
				capture("main")

		suite = unittest.makeSuite(Foo)
		result = unittest.TestResult()
		suite.run(result)

		self.assertTrue(result.wasSuccessful())
		self.assertEqual(lines, ['_setup', 'setup', 'main', '_teardown', 'teardown'])
		
		core._setup = backup_setup
		core._teardown = backup_teardown
Пример #3
0
	def test_frozen(self):
		wrapper = mock().frozen().unfrozen()
		wrapper.raw.child_a = 'baz'
		self.assertEqual(wrapper.raw.child_a, 'baz')
		
		wrapper = mock().frozen()
		self.assert_mock_is_frozen(wrapper)
Пример #4
0
	def test_invalid_usage_after_teardown(self):
		core._teardown()
		
		e = None
		try:
			mock()
		except Exception, e_:
			e = e_
Пример #5
0
	def test_default_return_value(self):
		wrapper = mock()
		mock_ = wrapper.raw
		self.assertTrue(wrapper.return_value is mocktest.silentmock.DEFAULT)
		retval = mock_()
		self.is_a_mock(retval)
		self.assertEqual(mock(retval).name, 'return value for (unnamed mock)')
		self.is_a_mock(wrapper.return_value)
Пример #6
0
	def test_raising(self):
		# class
		wrapper = mock().raising(SystemError)
		self.assertRaises(SystemError, wrapper.raw)
	
		# instance (with random extra args)
		wrapper = mock().raising(SystemError("this simply will not do"))
		self.assertRaises(SystemError, lambda: wrapper.raw('a','b',foo='blah'))
Пример #7
0
	def test_ignore(self):
		callback = raw_mock()
		mock(callback).is_expected.exactly(0).times
			
		@ignore
		def test_failure(self):
			callback('a')
		
		self.assert_(self.run_method(test_failure).wasSuccessful())
		assert_desc(self.output.called.with_('[[[ IGNORED ]]] ... '))
Пример #8
0
	def test_should_ignore_with_description(self):
		callback = raw_mock()
		mock(callback).is_expected.exactly(0).times
			
		@ignore('not done yet')
		def test_failure(self):
			callback('a')
		
		self.assert_(self.run_method(test_failure).wasSuccessful())
		assert_desc(self.output.called.with_('[[[ IGNORED ]]] (not done yet) ... '))
Пример #9
0
 def failing_mock_expectation(slf):
     expect(mock()).foo
     # emulate a refresh
     try:
         core._teardown()
     finally:
         core._setup()
Пример #10
0
    def test_reality_formatting(self):
        core._teardown()
        try:
            with MockTransaction:
                m = mock('meth')
                expect(m).meth.once()
                m.meth(1, 2, 3)
                m.meth(foo='bar')
                m.meth()
                m.meth(1, foo=2)
        except AssertionError as e:
            line_agnostic_repr = [
                re.sub('\.py:[0-9 ]{3} ', '.py:LINE ', line)
                for line in str(e).split('\n')
            ]

            expected_lines = [
                'Mock "meth" did not match expectations:',
                ' expected exactly 1 calls',
                ' received 4 calls with arguments:',
                '  1:   (1, 2, 3)                // mocktest_test.py:LINE :: m.meth(1,2,3)',
                "  2:   (foo='bar')              // mocktest_test.py:LINE :: m.meth(foo='bar')",
                '  3:   ()                       // mocktest_test.py:LINE :: m.meth()',
                '  4:   (1, foo=2)               // mocktest_test.py:LINE :: m.meth(1, foo=2)'
            ]

            for got, expected in zip(line_agnostic_repr, expected_lines):
                self.assertEqual(got, expected)
        finally:
            core._setup()
Пример #11
0
	def test_spec_class_in_constructor(self):
		wrapper = mock().with_spec(self.SpecClass)
		self.assertEqual(wrapper._children.keys(), ['a','b'])
		self.is_a_mock(wrapper.raw.a())
		self.assert_mock_is_frozen(wrapper)
		
		self.assertRaises(AttributeError, lambda: wrapper.raw.__something__)
Пример #12
0
	def test_reality_formatting(self):
		core._teardown()
		try:
			with MockTransaction:
				m = mock('meth')
				expect(m).meth.once()
				m.meth(1,2,3)
				m.meth(foo='bar')
				m.meth()
				m.meth(1, foo=2)
		except AssertionError, e:
			line_agnostic_repr = [
				re.sub('\.py:[0-9 ]{3} ', '.py:LINE ', line)
				for line in str(e).split('\n')]
			
			expected_lines = [
				'Mock "meth" did not match expectations:',
				' expected exactly 1 calls',
				' received 4 calls with arguments:',
				'  1:   (1, 2, 3)                // mocktest_test.py:LINE :: m.meth(1,2,3)',
				"  2:   (foo='bar')              // mocktest_test.py:LINE :: m.meth(foo='bar')",
				'  3:   ()                       // mocktest_test.py:LINE :: m.meth()',
				'  4:   (1, foo=2)               // mocktest_test.py:LINE :: m.meth(1, foo=2)']
			
			for got, expected in zip(line_agnostic_repr, expected_lines):
				self.assertEqual(got, expected)
Пример #13
0
		def failing_mock_expectation(slf):
			expect(mock()).foo
			# emulate a refresh
			try:
				core._teardown()
			finally:
				core._setup()
Пример #14
0
	def test_pending(self):
		callback = raw_mock()
			
		@pending
		def test_failure(self):
			callback('a')
			raise RuntimeError, "something went wrong!"
		
		self.assert_(self.run_method(test_failure).wasSuccessful())
		assert_desc(self.output.called.with_('[[[ PENDING ]]] ... '))
		
		@pending("reason")
		def test_failure_with_reason(self):
			assert False
			callback('b')
		
		self.assert_(self.run_method(test_failure_with_reason).wasSuccessful())
		assert_desc(self.output.called.with_('[[[ PENDING ]]] (reason) ... '))
		
		@pending
		def test_successful_pending_test(self):
			assert True
			callback('c')
		
		self.assertEqual(self.run_method(test_successful_pending_test).wasSuccessful(), False)
		assert_desc(self.output.called.with_('test_successful_pending_test PASSED unexpectedly '))
		
		self.assertEqual(mock(callback).called.exactly(2).times.get_calls(), [('a',), ('c',)])
Пример #15
0
	def test_should_show_where_calls_were_made(self):
		wrapper = mock()
		mock_ = wrapper.raw
		
		mock_(1,2,345)
		print str(wrapper.called)
		self.assertTrue(re.search('// mock_test\.py:[0-9]+ +:: mock_\(1,2,345\)', str(wrapper.called)))
Пример #16
0
	def test_expectation_formatting(self):
		self.assertEqual(
			repr(mock().called.with_('foo', bar=1).twice()),
			'\n'.join([
				'Mock "unnamed mock" did not match expectations:',
				' expected exactly 2 calls with arguments equal to: \'foo\', bar=1',
				' received 0 calls'])
		)
Пример #17
0
    def test_ignore(self):
        callback = mock()
        expect(callback).__call__.never()

        @ignore
        def test_failure(self):
            callback('a')

        self.assert_(self.run_method(test_failure).wasSuccessful())
Пример #18
0
    def test_should_ignore_with_description(self):
        callback = mock()
        expect(callback).__call__.never()

        @ignore('not done yet')
        def test_failure(self):
            callback('a')

        self.assert_(self.run_method(test_failure).wasSuccessful())
Пример #19
0
	def test_should_sync_unique_instapaper_urls(self):
		ipaper = mt.mock()
		app_globals.URLSAVE = ipaper.raw
		ipaper.expects('add_urls').with_(set(['url1', 'url2']))
		
		item = Item(item_with_title('blah'))
		item.instapaper_url = 'url1|url2|url2'
		item.is_dirty = True
		item.save_to_web()
Пример #20
0
	def test_should_allow_setting_of_magic_methods(self):
		clean_wrapper = mock().named('clean')
		modified_wrapper = mock().named('modified')
		
		modified_wrapper.method('__repr__').returning('my repr!')
		modified_wrapper.method('__str__').returning('my str!')
		modified_wrapper.method('__len__').returning(5)
		
		self.assertNotEqual(type(clean_wrapper.raw), type(modified_wrapper.raw))
		self.assertEqual(type(clean_wrapper.raw).__name__, type(modified_wrapper.raw).__name__)
		
		val = repr(modified_wrapper.raw)
		self.assertEqual(val, 'my repr!')
		self.assertTrue(modified_wrapper.child('__repr__').called.once())
	
		str_val = str(modified_wrapper.raw)
		self.assertEqual(str_val, 'my str!')
		self.assertTrue(modified_wrapper.child('__str__').called.once())
Пример #21
0
	def test_should_ignore_with_description(self):
		callback = mock()
		expect(callback).__call__.never()
			
		@ignore('not done yet')
		def test_failure(self):
			callback('a')
		
		self.assert_(self.run_method(test_failure).wasSuccessful())
Пример #22
0
	def test_should_do_expectations(self):
		f = mock()
		f.expects('foo').once()
		f.raw.foo('a')
		f.raw.foo()
		self.assertRaises(AssertionError, core._teardown, matching=re.compile('Mock "foo" .*expected exactly 1 calls.* received 2 calls.*', re.DOTALL))
		
		# pretend we're in a new test (wipe the expected calls register)
		core.MockWrapper._all_expectations = []
Пример #23
0
	def setUp(self):
		
		self.actually_called = False
		def called(whoami, *args, **kwargs):
			print "ACTUALLY CALLED"
			whoami.actually_called = True
			return 'real_return'
		
		self.wrapper = mock(called).returning('mock_return')
Пример #24
0
	def test_ignore(self):
		callback = mock()
		expect(callback).__call__.never()
			
		@ignore
		def test_failure(self):
			callback('a')
		
		self.assert_(self.run_method(test_failure).wasSuccessful())
Пример #25
0
	def test_should_do_expectations(self):
		try:
			with MockTransaction:
				f = mock()
				expect(f).foo.once()
				f.foo('a')
				f.foo()
			raise RuntimeError("should not reach here!")
		except AssertionError, e:
			assert re.compile('Mock "foo" .*expected exactly 1 calls.* received 2 calls.*', re.DOTALL).match(str(e)), str(e)
Пример #26
0
	def test_with_children_should_set_return_values_and_freeze_mock(self):
		wrapper = mock().with_children('blob', foo='bar', x=123)
		mock_ = wrapper.raw
		
		self.is_a_mock(mock_.blob)
		self.assertEqual(mock_.foo, 'bar')
		self.assertEqual(mock_.x, 123)
		
		self.assertEqual(sorted(wrapper._children.keys()), ['blob', 'foo','x'])
		self.assert_mock_is_frozen(wrapper)
Пример #27
0
	def test_is_not_expected(self):
		wrapper = mock()
		mock_ = wrapper.raw
		
		expect(mock_.a).once()
		wrapper.child('b').is_not_expected
		mock_.a()
		
		core._teardown()
		core._setup()
Пример #28
0
	def test_should_delete_unneeded_pagefeed_items(self):
		ipaper = mt.mock()
		app_globals.URLSAVE = ipaper.raw
		mt.mock_on(app_globals.READER).set_read
		ipaper.expects('delete').with_(url='url')
		
		item = Item(item_with(link='url'))
		item.is_pagefeed = True
		item.is_dirty = True
		item.is_read = True
		item.save_to_web()
Пример #29
0
	def test_should_not_delete_needed_pagefeed_items(self):
		ipaper = mt.mock()
		app_globals.URLSAVE = ipaper.raw
		mt.mock_on(app_globals.READER).add_star
		ipaper.method('delete').returning('OK').is_not_expected
		
		item = Item(item_with(url='url'))
		item.is_pagefeed = True
		item.is_starred = True
		item.is_dirty = True
		item.save_to_web()
Пример #30
0
	def test_constructor(self):
		mock_ = raw_mock()
		wrapper = mock(mock_)
		self.assertFalse(wrapper.called, "called not initialised correctly")
		self.assertTrue(wrapper.called.exactly(0), "call_count not initialised correctly")
	
		self.assertEquals(wrapper.call_list, [])
		self.assertEquals(wrapper._children, {})
		self.assertEquals(wrapper.action, None)
		self.assertEquals(wrapper.name, 'unnamed mock')
		wrapper.name = 'lil bobby mock'
Пример #31
0
	def test_with_methods_should_set_return_values_and_freeze_mock(self):
		wrapper = mock().with_methods('blob', '_blob', foo='bar', x=123, _blobx='underscore!')
		mock_ = wrapper.raw
		
		self.is_a_mock(mock_.blob())
		self.is_a_mock(mock_._blob())
		self.assertEqual(mock_.foo(), 'bar')
		self.assertEqual(mock_.x(), 123)
		self.assertEqual(mock_._blobx(), 'underscore!')
		
		self.assertEqual(sorted(wrapper._children.keys()), ['_blob', '_blobx', 'blob', 'foo','x'])
		self.assert_mock_is_frozen(wrapper)
Пример #32
0
 def test_should_do_expectations(self):
     try:
         with MockTransaction:
             f = mock()
             expect(f).foo.once()
             f.foo('a')
             f.foo()
         raise RuntimeError("should not reach here!")
     except AssertionError as e:
         assert re.compile(
             'Mock "foo" .*expected exactly 1 calls.* received 2 calls.*',
             re.DOTALL).match(str(e)), str(e)
Пример #33
0
	def test_reality_formatting(self):
		m = mock().named('ze_mock').raw
		m(1,2,3)
		m(foo='bar')
		m()
		m(1, foo=2)
		
		line_agnostic_repr = [
			re.sub('\.py:[0-9 ]{3} ', '.py:LINE ', line)
			for line in repr(mock(m).called.once()).split('\n')]
		
		expected_lines = [
			'Mock "ze_mock" did not match expectations:',
			' expected exactly 1 calls',
			' received 4 calls with arguments:',
			'  1:   1, 2, 3                  // mocktest_test.py:LINE :: m(1,2,3)',
			"  2:   foo='bar'                // mocktest_test.py:LINE :: m(foo='bar')",
			'  3:   No arguments             // mocktest_test.py:LINE :: m()',
			'  4:   1, foo=2                 // mocktest_test.py:LINE :: m(1, foo=2)']
		
		for got, expected in zip(line_agnostic_repr, expected_lines):
			self.assertEqual(got, expected)
Пример #34
0
	def test_expect_should_work_on_a_mock_or_wrapper(self):
		wrapper = mock()
		mock_ = wrapper.raw
		
		expect(mock_.a).once()
		wrapper.expects('b').once()
		wrapper.child('c').is_expected.once()

		self.assertTrue(len(core.MockWrapper._all_expectations) == 3)
		
		mock_.a()
		mock_.b()
		mock_.c()
Пример #35
0
	def test_invalid_usage_after_teardown(self):
		core._teardown()
		try:
			e = None
			try:
				m = mock()
				expect(m).foo().never()
			except Exception, e_:
				print repr(e_)
				e = e_

			self.assertFalse(e is None, "no exception was raised")
			self.assertEqual(str(e), "Mock transaction has not been started. Make sure you are inheriting from mocktest.TestCase")
			self.assertEqual(e.__class__, AssertionError)
Пример #36
0
    def test_invalid_usage_after_teardown(self):
        core._teardown()
        try:
            e = None
            try:
                m = mock()
                expect(m).foo().never()
            except Exception as e_:
                print(repr(e_))
                e = e_

            self.assertFalse(e is None, "no exception was raised")
            self.assertEqual(
                str(e),
                "Mock transaction has not been started. Make sure you are inheriting from mocktest.TestCase"
            )
            self.assertEqual(e.__class__, AssertionError)
        finally:
            core._setup()
Пример #37
0
 def test_a(self):
     foo = mock()
     expect(foo).blah.once()
Пример #38
0
 def mock_failure(slf):
     expect(mock()).foo().at_least.once()