示例#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')