模拟记录异常
我正在使用 mock.record()
并且正在检查文件名的长度.. 测试代码为:
MockRepository mock = new MockRepository();
IExtension ext = mock.StrictMock<IExtension>();
using (mock.Record())
{
ext.LogError("filename not valid");
}
LogAnalyser log = new LogAnalyser(ext);
string shortfilename = "jjh.df";
log.IsValid(shortfilename);
mock.Verify(ext);
生产代码为:
public void IsValid(string filename)
{
if(filename.Length<8)
{
extension.LogError("filename is short:" + filename);
}
}
调试时,生产中的 extension.logerror
给出异常:
IExtension.LogError("filename is short:jjh.df"); Expected #0, Actual #1.
IExtension.LogError("filename not valid"); Expected #1, Actual #0.
请提供一些解决方案。
I am using mock.record()
and I am checking the length of a filename..
The test code is:
MockRepository mock = new MockRepository();
IExtension ext = mock.StrictMock<IExtension>();
using (mock.Record())
{
ext.LogError("filename not valid");
}
LogAnalyser log = new LogAnalyser(ext);
string shortfilename = "jjh.df";
log.IsValid(shortfilename);
mock.Verify(ext);
and the production code is:
public void IsValid(string filename)
{
if(filename.Length<8)
{
extension.LogError("filename is short:" + filename);
}
}
On debugging, the extension.logerror
in production gives an exception:
IExtension.LogError("filename is short:jjh.df"); Expected #0, Actual #1.
IExtension.LogError("filename not valid"); Expected #1, Actual #0.
please provide some solution.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
LogError 被击中不止一次。添加以下内容:
我不能 100% 确定您是否需要
The LogError is hit more then one time. Add the following:
I am not 100% sure if you need the
您使用的是 StrictMock,这意味着只允许您专门设置的调用 - 并且包括参数的值。您设置了一个期望,即使用字符串调用 LogError:
但在执行过程中实际发生的是使用字符串进行调用:
所以 Rhino.Mocks 给您两个异常(一个是因为它发现了一个不存在的调用) t 是预期的,另一个是因为未找到预期的调用)。
@Ivo 使用“IgnoreArguments”的响应应该有效。如果不是,请提供有关您正在使用的 .NET 和 Rhino.Mocks 版本的更多详细信息。
You're using a StrictMock which means only calls specifically set up by you are allowed -- and that includes the values of the parameters. You set an expectation that a call would be made to LogError with the string:
But what actually happened during execution was a call was made with the string:
So Rhino.Mocks gives you the two exceptions (one because it found a call that wasn't expected and the other because an expected call was not found).
@Ivo's response of using "IgnoreArguments" should work. If it's not, please include more details as to what version of .NET and Rhino.Mocks you're using.