diff --git a/FizzBuzz.py b/FizzBuzz.py index 00b6ebd..dc4b397 100644 --- a/FizzBuzz.py +++ b/FizzBuzz.py @@ -1,18 +1,21 @@ """ Q1. Why is the report method untestable ? [2 pts] - +External collaborator directly file pointer is given.instead wrapper func should be used - FileHandleWrapper or pass an arg as fileopener. Q2. How will you change the api of the report method to make it more testable ? [2 pts] +as mentioned above.changes done in below code : + """ class FizzBuzz(object): - def report(self, numbers): + def report(self, numbers, FileOpener): + FileOpener.Open() - report_file = open('c:/temp/fizzbuzz_report.txt', 'w') + report_file = FileOpener('c:/temp/fizzbuzz_report.txt', 'w') for number in numbers: msg = str(number) + " " diff --git a/TestFizzBuzzMocked.py b/TestFizzBuzzMocked.py index 6f1d0d5..18c3cb2 100644 --- a/TestFizzBuzzMocked.py +++ b/TestFizzBuzzMocked.py @@ -17,24 +17,22 @@ def tearDown(self): self.fb = None def test_report(self): - pass + #mock the file handle + MockInter = self.mock() + MockFile = self.mock() + self.expectAndReturn(MockInter.open('c:/temp/fizzbuzz_report.txt', 'w'), MockFile) + MockFile.write("3 fizz \n") + MockFile.close() + #replay + self.replay() + #call API + self.fb.report(numbers, FileOpener=MockInter.open) - - - - - - - - - - - - - + # verify + self.verify() if __name__ == "__main__": unittest.main() diff --git a/TestFizzBuzzStubbed.py b/TestFizzBuzzStubbed.py index 78ee454..1fb4f28 100644 --- a/TestFizzBuzzStubbed.py +++ b/TestFizzBuzzStubbed.py @@ -4,7 +4,14 @@ """ Q3. What will be printed when we execute 'python FizzBuzzStubbed.py' ? [3 pts] - +#class setup - at start setUp +Class FizzBuzzStubbed +#test setup before each testsetup +test_reportteardown +#test setup before each test.setup +test_reportteardown +#class teardown - at end +.tearDownClass @@ -17,16 +24,16 @@ """ class MyStub(object): - pass - - - - - + def __init__(self): + self.values = [] + def write(self, value): + self.values.append(value) + def close(self): + self.closed = True - + class TestFizzBuzzStubbed(unittest.TestCase): @classmethod