diff --git a/FizzBuzz.py b/FizzBuzz.py index 00b6ebd..ab7d043 100644 --- a/FizzBuzz.py +++ b/FizzBuzz.py @@ -1,13 +1,55 @@ """ Q1. Why is the report method untestable ? [2 pts] - +The report method is using the open command which is an external collaborator, the external collaborator should +be passed as an argument to the report method Q2. How will you change the api of the report method to make it more testable ? [2 pts] +The defintion of report will accept three arguments report_file(self, numbers,fileWrapper) +the calls to open , write and close will be substituted by the fileWrapper. +This wrapper can be mocked and code logic can be tested accordingly. + + +class FizzBuzz(object): + def report(self, numbers,filehandle): + + filehandle.open() + + 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_numbersfound = True + + if fizzbuzz_found: + filehandle.write(msg + "\n") + + filehandle.close() + +if "__main__" == __name__: + fb = FizzBuzz() + wrapper = filewrapper('fizzbuzz_report.txt', 'w') + fb.report(range(100),wrapper) + +class filewrapper: + def __init__(self,fname,mode): + self.fname = fname + self.mode = mode + def open(self): + self.fh = open(self.fname,self.mode) + return self.fh + def write(self,msg): + self.fh.write(msg) + def close(self): + self.fh.close() """ class FizzBuzz(object): def report(self, numbers): diff --git a/TestFizzBuzzMocked.py b/TestFizzBuzzMocked.py index 6f1d0d5..9e07faf 100644 --- a/TestFizzBuzzMocked.py +++ b/TestFizzBuzzMocked.py @@ -4,6 +4,20 @@ """ Q5. Write the psuedocode for the test_repport method, such that it uses PyMock mock objects to test the report method of FizzBuzz. [5 pts] + + def test_report(self): + #mock file wrapper + mockFileProvider = self.mock() + mockFileWrapper = self.mockFileProvider('fizzbuzz_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() + """ class TestFizzBuzzMocked(pymock.PyMockTestCase): diff --git a/TestFizzBuzzStubbed.py b/TestFizzBuzzStubbed.py index 78ee454..506b04e 100644 --- a/TestFizzBuzzStubbed.py +++ b/TestFizzBuzzStubbed.py @@ -4,10 +4,15 @@ """ Q3. What will be printed when we execute 'python FizzBuzzStubbed.py' ? [3 pts] - - - - +setUpClass FizzBuzzStubbed +setup +test_report +teardown +test_report +setup +test_report +teardown +tearDownClass @@ -15,6 +20,18 @@ Q4. Implement MyStub class so that you can send it as a fake object to the report method of FizzBuzz object from a test case. [3 pts] +external contributer for file operations needs to be faked in the MyStub class, this includes faking open,write and close operations + +class MyStub(object): + def __init__(self): + self.openflag=False + self.values=[] # create empty list + def open(self): + self.openflag = True #"open fake file" + def write(self,msg): + self.values.append(msg) # append values to list + def close(): + self.openflag = False # close fake "file" """ class MyStub(object): pass