From 64b07cf930dc83cfbd14137b441570cbd93a74cf Mon Sep 17 00:00:00 2001 From: ashishdeshpande Date: Tue, 26 Jun 2012 15:56:00 +0530 Subject: [PATCH 1/4] Update master --- FizzBuzz.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/FizzBuzz.py b/FizzBuzz.py index 00b6ebd..c4484b4 100644 --- a/FizzBuzz.py +++ b/FizzBuzz.py @@ -1,11 +1,15 @@ """ 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. """ From 5f49e7a30e19928eab68fb16d44b6cc74e54453c Mon Sep 17 00:00:00 2001 From: ashishdeshpande Date: Wed, 27 Jun 2012 09:17:33 +0530 Subject: [PATCH 2/4] Update master --- TestFizzBuzzStubbed.py | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) 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 From 4cc8705534fb446b02f894725f253879a80c4d31 Mon Sep 17 00:00:00 2001 From: ashishdeshpande Date: Wed, 27 Jun 2012 09:25:20 +0530 Subject: [PATCH 3/4] Update master --- FizzBuzz.py | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/FizzBuzz.py b/FizzBuzz.py index c4484b4..ab7d043 100644 --- a/FizzBuzz.py +++ b/FizzBuzz.py @@ -12,6 +12,44 @@ 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): From c6d31ce8ab871be755bba7214b1a0fcce2cf73b1 Mon Sep 17 00:00:00 2001 From: ashishdeshpande Date: Wed, 27 Jun 2012 10:18:05 +0530 Subject: [PATCH 4/4] Update master --- TestFizzBuzzMocked.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) 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):