diff --git a/FizzBuzz.py b/FizzBuzz.py index 00b6ebd..dced480 100644 --- a/FizzBuzz.py +++ b/FizzBuzz.py @@ -1,6 +1,7 @@ """ Q1. Why is the report method untestable ? [2 pts] - +......... +report_file open and path is external collaborative (platform and system environment dependent) which is handled in the function so it is untestable @@ -10,27 +11,46 @@ """ class FizzBuzz(object): - def report(self, numbers): - report_file = open('c:/temp/fizzbuzz_report.txt', 'w') + def report(self, numbers, file_handle): for number in numbers: + msg = str(number) + " " + fizzbuzz_found = False + if number % 3 == 0: + msg += "fizz " + fizzbuzz_found = True + if number % 5 == 0: - msg += "buzz " - fizzbuzz_found = True + + msg += "buzz " + + fizzbuzz_found = True if fizzbuzz_found: - report_file.write(msg + "\n") - report_file.close() + file_handle.write(msg + "\n") -if "__main__" == __name__: - fb = FizzBuzz() - fb.report(range(100)) + + + + + + +if "__main__" == __name__: + + fb = FizzBuzz() + + file_handle = open('temp.txt', 'w') # can create open wrapper + + fb.report(range(100), file_handle) + + file_handle.close() + diff --git a/TestFizzBuzzMocked.py b/TestFizzBuzzMocked.py index 6f1d0d5..331af1b 100644 --- a/TestFizzBuzzMocked.py +++ b/TestFizzBuzzMocked.py @@ -17,8 +17,16 @@ def tearDown(self): self.fb = None def test_report(self): - pass - + #create mock + mockFileProvider = self.mock() + mockFileWrapper = self.mockFileProvider('report.txt', 'w') + #replay + self.replay() + #Call the report method with mocked filewrapper + numbers=range(100) + FizzBuzz.report(self.numbers,self.mockFileWrapper) + #verify + self.verify() diff --git a/TestFizzBuzzStubbed.py b/TestFizzBuzzStubbed.py index 78ee454..a0a451b 100644 --- a/TestFizzBuzzStubbed.py +++ b/TestFizzBuzzStubbed.py @@ -3,12 +3,16 @@ import FizzBuzz """ Q3. What will be printed when we execute 'python FizzBuzzStubbed.py' ? [3 pts] +.............. - - - - - +setUpClass FizzBuzzStubbed +setup +test_report +teardown +.setup +test_report +teardown +.tearDownClass @@ -17,16 +21,20 @@ """ class MyStub(object): - pass - - - - - - - + def __init__(self, values) + self.flag = False + self.values = [] + + def open(self) + self.flag = True + + def write(self,msg) + self.values.append(msg) + + def close(self) + self.flag = False - + class TestFizzBuzzStubbed(unittest.TestCase): @classmethod