Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

  1. Line 13 constrains the testing of this method. We can only test all paths if the now variable holds a time that is less than 2000 hours

  2. Line 16, the dependant dependent class could cause an issue if it performs IO, Network, and DB calls.

...

Line 13 can be resolved by refactoring the code so that Lines 10-11 are in a method of their own, and calling that method in Line 13 if( getTime() < stopTime )

Activities

Info

If you are stuck at any time, use the code in the why-spy.zip and use git tag and git checkout <tag> to see how the code evolves

  1. Refactoring the code and writing a new test to support the refactored code - read Activity 1 Observations

    1. Refactor the code so that the FileLog is injected into the DataClerk

    2. Decide how you can test it to verify that the code is still working.

    3. Give attention to whether you should be injecting the class or an interface

    4. Use the NSubstitute When... Do... or Received() (or a library you are familiar with that has the same capabilities) feature to set up an expectation that can be tested in an Assert.

  2. Line 11 is an issue. The timestamp is hard coded. Not a problem you say. We can remove the magic number so it’s declared as a constant field in the class. This doesn’t really help us because we can only test alternative paths after 8pm. We can use a Spy mechanism to solve this. This can be done by refactoring the code so that the current time is returned from a private method in the DataClerk class.

    1. Turn line 10 into a private virtual public function

    2. Use the private function to get the time

    3. In the test use NSubstitute Substitute.ForPartsOf<StubClass>ForPartsOf<Class>(); to override the returned time so it is greater than 2000 hours. - see NSubstitue API Guide

  3. An alternative design for activity 2 would be to use a lambda expression in line 13

    1. Think about how this might work, and what would the test look like?

Info

git repo