From ce612e5ed4743b8261578c2ea67c5b5dc51664c5 Mon Sep 17 00:00:00 2001 From: sumit-modi Date: Wed, 27 Jun 2012 13:39:14 +0530 Subject: [PATCH 1/4] Update master --- FizzBuzz.py | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/FizzBuzz.py b/FizzBuzz.py index 00b6ebd..f2baa48 100644 --- a/FizzBuzz.py +++ b/FizzBuzz.py @@ -1,18 +1,29 @@ """ +""" Q1. Why is the report method untestable ? [2 pts] - +It has external dependecies. +1. File operations +2. take file name as argument Q2. How will you change the api of the report method to make it more testable ? [2 pts] +make 'open' function and file name as an argument to report like : + +def report(self, numbers, file_name, opener=open) + + + +""" + """ class FizzBuzz(object): - def report(self, numbers): + def report(self, numbers, fileHandler): - report_file = open('c:/temp/fizzbuzz_report.txt', 'w') + #report_file = open('c:/temp/fizzbuzz_report.txt', 'w') for number in numbers: msg = str(number) + " " From 7953e347c2379305567d953f79eb09ee7e0e87f6 Mon Sep 17 00:00:00 2001 From: sumit-modi Date: Wed, 27 Jun 2012 13:52:58 +0530 Subject: [PATCH 2/4] Update master --- FizzBuzz.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/FizzBuzz.py b/FizzBuzz.py index f2baa48..c565d93 100644 --- a/FizzBuzz.py +++ b/FizzBuzz.py @@ -9,9 +9,7 @@ Q2. How will you change the api of the report method to make it more testable ? [2 pts] -make 'open' function and file name as an argument to report like : - -def report(self, numbers, file_name, opener=open) +Give the file handler to report method. From 6fc267e4b9b4014ab0af284a26a91332fe5e9da0 Mon Sep 17 00:00:00 2001 From: sumit-modi Date: Wed, 27 Jun 2012 13:55:05 +0530 Subject: [PATCH 3/4] Update master --- TestFizzBuzzStubbed.py | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/TestFizzBuzzStubbed.py b/TestFizzBuzzStubbed.py index 78ee454..68c2c5c 100644 --- a/TestFizzBuzzStubbed.py +++ b/TestFizzBuzzStubbed.py @@ -6,18 +6,39 @@ - +Ans:_ +setUpClass FizzBuzzStubbed +setup +test_report +teardown +.setup +test_report +teardown +.tearDownClass 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] +report method of FizzBuzz object from a test case. [3 pts] """ class MyStub(object): - pass + 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" + + @@ -56,4 +77,4 @@ def test_report_for_empty_list(self): pass if __name__ == "__main__": - unittest.main() + unittest.main() \ No newline at end of file From 9b5a027206ed93a9b04b0fe94b83ffa64a5d10cf Mon Sep 17 00:00:00 2001 From: sumit-modi Date: Wed, 27 Jun 2012 13:57:04 +0530 Subject: [PATCH 4/4] Update master --- TestFizzBuzzMocked.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/TestFizzBuzzMocked.py b/TestFizzBuzzMocked.py index 6f1d0d5..65b9536 100644 --- a/TestFizzBuzzMocked.py +++ b/TestFizzBuzzMocked.py @@ -17,7 +17,21 @@ def tearDown(self): self.fb = None def test_report(self): - pass + + mock_opener = self.mock() + mock_report_file = self.mock() + self.expectAndReturn(mock_opener.open('c:\temp\Joe.txt', 'w'), mock_report_file) + + mock_report_file.write("33 fizz") + mock_report_file.close() + #replay + self.replay() + + fb = FizzBuzz.FizzBuzz() + fb.report([33], 'c:\temp\Joe.txt', mock_report_file) + + #verify + self.verify()