Check your understanding

Take a look at the code for this simple class, and use it to figure out the answers to the questions. It's okay to check back through the book. This is just a way for you to make sure you're ready to move on:

class ClassOne:
    def __init__(self, arg1, arg2):
        self.arg1 = int(arg1)
        self.arg2 = arg2

    def method1(self, x):
        return x * self.arg1

    def method2(self, x):
        return self.method1(self.arg2) * x

Here are the questions:

  1. Assuming that we're using methods as units, how many units exist in the preceding code?

    Answer: There are three units that exist in the preceding code and that are as follows: __init__, method1, and method2. __init__ is a method, just as method1 and method2. The fact that it's a constructor means that it's all tangled up with the other units, but it's still a method containing code and a possible location for bugs, and so we cannot afford to treat this as anything other than a unit.

  2. Which units make assumptions about the correct operation of other units? In other words, which units are not independent?

    Answer: Both method1 and method2 assume that __init__ works right, and method2 makes the same assumption as that of method1.

  3. How can you write a test for method2 that does not assume that other units work correctly?

    Answer: The tests for method2 will need to use a fake method1 that is a part of the test code, and not a part of the code being tested.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.202.61