diff --git a/FizzBuzz.py b/FizzBuzz.py index 00b6ebd..0aed186 100644 --- a/FizzBuzz.py +++ b/FizzBuzz.py @@ -1,18 +1,22 @@ """ Q1. Why is the report method untestable ? [2 pts] - +report_file takes pointer of file , available on local machine- external collaborators Q2. How will you change the api of the report method to make it more testable ? [2 pts] - +Passing a Filehandlewrapper to report function , so that function should not pick his collaborators. +We can use - +a. Filehandlewrapper - a wrapper class defined to have interface function for OPen +b. fileopener = using argument in report function, Implemented below """ class FizzBuzz(object): - def report(self, numbers): + def report(self, numbers,fileopener=open): - report_file = open('c:/temp/fizzbuzz_report.txt', 'w') + #report_file = Filehandlewrapper.open + 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..a7fb09a 100644 --- a/TestFizzBuzzMocked.py +++ b/TestFizzBuzzMocked.py @@ -17,7 +17,22 @@ def tearDown(self): self.fb = None def test_report(self): - pass + #mock the file handle + mock_opener_interface = self.mock() + mock_file = self.mock() + self.expectAndReturn(mock_opener_interface.open('c:/temp/fizzbuzz_report.txt', 'w'), mock_file) + mock_file.write("3 fizz \n") + mock_file.close() + + #replay + self.replay() + + #call API + self.fb.report(numbers, fileOpener=mock_opener_interface.open) + + # verify + self.verify() + diff --git a/TestFizzBuzzStubbed.py b/TestFizzBuzzStubbed.py index 78ee454..78b048d 100644 --- a/TestFizzBuzzStubbed.py +++ b/TestFizzBuzzStubbed.py @@ -3,11 +3,19 @@ import FizzBuzz """ Q3. What will be printed when we execute 'python FizzBuzzStubbed.py' ? [3 pts] +#class setup - at start +setUpClass FizzBuzzStubbed - - - - +#test setup before each test +setup +test_report +teardown +#test setup before each test +.setup +test_report +teardown +#class teardown - at end +.tearDownClass @@ -16,8 +24,17 @@ report method of FizzBuzz object from a test case. [3 pts] """ + class MyStub(object): - pass + + def __init__(self): + self.values = [] + + def write(self, value): + self.values.append(value) + + def close(self): + self.closed = True