Unit Testing a large method

后端 未结 5 1544
说谎
说谎 2020-12-18 01:36

Following Test-Driven Development that is.

I\'ve recently implemented a algorithm (A*) that required a clean interface. By clean all I want is a couple of propertie

5条回答
  •  南方客
    南方客 (楼主)
    2020-12-18 01:39

    Refactor your big method into smaller, private methods. Use reflection, or another mechanism available in your language, to test the smaller methods independently. Worst case -- if you don't have access to reflection or friends, make the methods protected and have your test class inherit from the main class so it can have access to them.

    Update: I should also clarify that simply refactoring to private methods doesn't necessarily imply that you need to create tests specific to those methods. If you are thoroughly testing the public methods that rely on the private methods, you may not need to test the private methods directly. This is probably the general case. There are times when it does make sense to test private methods directly (say when it simplifies or reduces the number of test cases needed for public methods), but I wouldn't consider it a requirement to create tests simply because you refactor to a private method implementation.

提交回复
热议问题