我使用Rhino Mocks尝试验证当我调用某个方法时,该方法反过来将正确地分组项目,然后调用另一种方法。检查参数是否正确使用的最佳测试模式是什么?
事情是这样的:
//Arrange
var bucketsOfFun = new BucketGame();
var balls = new List<IBall>
{
new Ball { Color = Color.Red },
new Ball { Color = Color.Blue },
new Ball { Color = Color.Yellow },
new Ball { Color = Color.Orange },
new Ball { Color = Color.Orange }
};
//Act
bucketsOfFun.HaveFunWithBucketsAndBalls(balls);
//Assert ???
这里就是麻烦的开始我。我的方法是做这样的事情:
public void HaveFunWithBucketsAndBalls(IList<IBall> balls)
{
//group all the balls together according to color
var blueBalls = GetBlueBalls(balls);
var redBalls = GetRedBalls(balls);
// you get the idea
HaveFunWithABucketOfBalls(blueBalls);
HaveFunWithABucketOfBalls(redBalls);
// etc etc with all the different colors
}
public void HaveFunWithABucketOfBalls(IList<IBall> colorSpecificBalls)
{
//doing some stuff here that i don't care about
//for the test i'm writing right now
}
我想断言的是,每次我打电话说我有一组1个红球,那么1个蓝色球调用它HaveFunWithABucketOfBalls,然后1个黄球,然后2个橙色的球。
如果我可以断言该行为,那么我可以验证该方法正在做我想做的事情,即正确地分组这些球。
任何想法是什么样的最好的测试模式?
只是为了方便,为什么你测试你的方法调用另一种方法?据推测,你应该只是测试你的对象的公共方法,并断言你得到正确的输出,你不应该测试实际的内部实现。 – Juliet 2010-04-12 23:15:18
@Juliet这是一个很好的观点,答案是因为这两种方法都是公开的。在我的实际实现中,它们都可以独立使用。所以两者之间唯一真正的区别是对象如何从我在这里描述的方法组织起来,这就是为什么我想测试这个功能。 – Joseph 2010-04-13 17:16:51