Wednesday 15 June 2011

c++ - How do I factor code to ease testability? -


I'm learning about unit testing and want to know how to write the write code. But, I'm not sure how to write a write-up code without complaining it, I will take the famous car and engine problem to describe the problem.

  class car {Private: engine m_engine; Public: car (); // the rest of the car}  

I came up with the following solutions to make the code above checkable.

  1. To replace the engine as a parameter, then replace the car maker then copy and test the engine. But, if I do not have different types of engines, then the constructor seems inappropriate to make the parameter, to make it only testable.

  2. Using a setter and then a duplicate engine flows in the same way as above the setter.

  3. To test the engine first and then test the car with a proven engine (or using a stub engine).

What are the options to test the code above? What are the strengths and weaknesses of each method?

If you have only one engine type, then you are trying to create a new object ? If you do not plan on swapping engines, then do not create another abstract layer just make the engine of the car part.

Instead of using the components again, you can be weak to reduce the complexity of good judgment. In which case, I can say that 3 is your best bet - validate your lower level components, then use a higher level code which calls the lower level object.

In reality, something like the engine database is likely to happen. And you want to change your constructor to use a different database (test reasons, or other reasons), but you can leave that lie for a while.


No comments:

Post a Comment