Let's say I have 开发者_Go百科a class which has a private void method. And I want to test this method by a some seed parameters for each specific iteration.
For instance, I want to make StartTrhoughGridCells method below testable for its iterations so that I can check statte changes in parameterObject.
In such cases, how can I make a each iteration testable in a private method that has a loop inside?
public class Traveller
{
// ... Some memebers, properties
private void StartTrhoughGridCells(object parameterObject)
{
// Do some state changes on parameterObject
}
}
Unit testing is black box testing, so conceptually, testing specific private methods should never be a goal in itself. The unit tests don't care about private methods and they don't know about their existence. Now, when it comes to code coverage you'd want to hit all of the code, but that's not quite the same...
When writing unit tests you only care about observable effects. Presumably the StartTrhoughGridCells method exists to produce some kind of observable effect, so you'll need to test that. However, from the question I can't tell what that observable effect would be, so you'll have to figure that out for yourself.
In other words you'd need to hit the method through whatever public API you have. If that's too hard to do, it's a sign that your API is too coarse-grained. In that case, consider whether some of the private methods can't be refactored to composable Strategies or similar.
If your problem is that the method is private
Here is a suggested workaround
Add public method inside if DEBUG
#id DEBUG
public void StartTrhoughGridCells_ForTestOnly(object parameterObject) // Workaround
{
StartTrhoughGridCells(parameterObject)
}
#endif
This way it won't exist in release Assuming your tests run on Debug version - they can test this method
精彩评论