犀牛嘲笑命名期望
我的测试对象有两个相同类型的依赖对象。有时,当测试的期望失败时,并不清楚哪个依赖对象设置了该期望。有没有某种方法可以给出将出现在错误消息中的依赖对象名称,以便我可以区分它们?
这是一个示例:
MockRepository mocks = new MockRepository();
var xAxis = mocks.StrictMock<IAxis>();
var yAxis = mocks.StrictMock<IAxis>();
Ball ball;
using (mocks.Record())
{
Expect.Call(xAxis.Velocity).Return(100);
Expect.Call(yAxis.Velocity).Return(0);
}
using (mocks.Playback())
{
ball = new Ball(xAxis, yAxis);
ball.Bounce();
}
现在,如果 Bounce 代码有问题,我可能会收到如下消息:
Rhino.Mocks.Exceptions.ExpectationViolationException: IAxis.get_Velocity();预期 #1,实际 #0。
我无法轻易判断哪个轴被错过了。
My object under test has two dependency objects of the same type. Sometimes when a test has a failed expectation, it's not clear which dependency object set that expectation. Is there some way to give the dependency objects names that will appear in the error messages so that I can tell them apart?
Here's an example:
MockRepository mocks = new MockRepository();
var xAxis = mocks.StrictMock<IAxis>();
var yAxis = mocks.StrictMock<IAxis>();
Ball ball;
using (mocks.Record())
{
Expect.Call(xAxis.Velocity).Return(100);
Expect.Call(yAxis.Velocity).Return(0);
}
using (mocks.Playback())
{
ball = new Ball(xAxis, yAxis);
ball.Bounce();
}
Now if there's something wrong with the Bounce code, I might get a message like this:
Rhino.Mocks.Exceptions.ExpectationViolationException :
IAxis.get_Velocity(); Expected #1, Actual #0.
I can't easily tell which axis got missed.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我找到了解决方案,但这并不完全是我所希望的。您可以为每个期望添加一条消息。我的例子变成:
现在异常更具描述性:
唯一的缺点是我必须为每个期望添加一条消息。我希望只命名模拟对象,以便该名称出现在所有消息中。
I found a solution, but it's not quite what I had hoped for. You can add a message to each expectation. My example becomes:
And the exception is now more descriptive:
The only down side is that I have to add a message for every expectation. I was hoping to just name the mock object so that that name would appear in all messages.