Пример #1
0
 def test_returns_mockablefile_if_missing_name_but_write(self):
     x = FileMocker()
     try:
         x('foo.txt', 'w')
     except IOError:
         self.fail("raised IOError even though 'w' mode")
Пример #2
0
 def test_can_be_used_with_patch(self):
     with mock.patch.object(builtins, 'open', FileMocker('foo')) as mo:
         x = open('anything.txt')
         self.assertIsInstance(x, MockableFile)
         self.assertEqual(x.read(), 'foo')
Пример #3
0
 def test_support_readline(self):
     x = FileMocker({'foo.txt': 'foo'})('foo.txt')
     self.assertEqual(x.readline(), 'foo')
     self.assertEqual(x.readline(), '')
Пример #4
0
 def test_supports_filepaths(self):
     x = FileMocker({'/path/foo.txt': 'foo', 'path/bar.txt': 'bar'})
     self.assertEqual(x('/path/foo.txt').read(), 'foo')
     self.assertEqual(x('path/bar.txt').read(), 'bar')
Пример #5
0
 def test_raises_ioerror_if_no_file(self):
     x = FileMocker({'foo.txt': 'bar'})
     self.assertRaises(IOError, x, 'bar.txt')
Пример #6
0
 def test_file_used_multiple_times(self):
     x = FileMocker({'foo.txt': 'foo'})
     self.assertEqual(x('foo.txt').read(), 'foo')
     self.assertEqual(x('foo.txt').read(), 'foo')
Пример #7
0
 def test_set_multiple_file_via_init(self):
     x = FileMocker({'foo.txt': 'foo', 'bar.txt': 'bar'})
     self.assertEqual(x('foo.txt').read(), 'foo')
     self.assertEqual(x('bar.txt').read(), 'bar')
Пример #8
0
 def test_set_single_string_contents_via_init(self):
     x = FileMocker('foo')
     self.assertEqual(x('anything.txt').read(), 'foo')
Пример #9
0
 def test_support_readline(self):
     x = FileMocker({'foo.txt': 'foo'})('foo.txt')
     self.assertEqual(x.readline(), 'foo')
     self.assertEqual(x.readline(), '')
Пример #10
0
 def test_set_multiple_file_via_set_contents(self):
     x = FileMocker()
     x.set_contents({'foo.txt': 'foo', 'bar.txt': 'bar'})
     self.assertEqual(x('foo.txt').read(), 'foo')
     self.assertEqual(x('bar.txt').read(), 'bar')
Пример #11
0
 def test_set_single_string_contents_via_set_contents(self):
     x = FileMocker()
     x.set_contents('foo')
     self.assertEqual(x('anything.txt').read(), 'foo')