Rhino Mocks - 在 Common.Logging ILog.Debug 上使用 AssertWasCalled
我在使用 Rhino Mocks 来断言调用了一个方法(最好是使用特定参数)时遇到了问题。该方法是 Common 中的 ILog.Debug(FormatMessageHandler)。使用新的amba语法记录2.0。使用旧方法普通 ILog.Debug(string) 可以正常工作。
// Sample Code to Test
public int TestFuncLambda(ILog log, int a, int b)
{
log.Debug(m => m("TestFunc START"));
int c = a + b;
log.Debug(m => m("TestFunc END"));
return c;
}
public int TestFunc(ILog log, int a, int b)
{
log.Debug("TestFunc START");
int c = a + b;
log.Debug("TestFunc END");
return c;
}
[TestMethod]
public void Should_log_start_TestFuncLamba()
{
var logger = MockRepository.GenerateMock<ILog>();
logger.Stub(x => x.IsDebugEnabled).Return(true);
TestFuncLambda(logger, 1, 2);
// Doesn't work, says zero calls plus I'm not sure how to check for the word "START" in the string either
logger.AssertWasCalled(x => x.Debug(Arg<FormatMessageHandler>.Is.Anything), o => o.IgnoreArguments());
}
[TestMethod]
public void Should_log_start_TestFunc()
{
var logger = MockRepository.GenerateMock<ILog>();
logger.Stub(x => x.IsDebugEnabled).Return(true);
TestFunc(logger, 1, 2);
// Works fine
logger.AssertWasCalled(x => x.Debug(Arg<string>.Matches(Text.Contains("START"))));
}
I'm having trouble using Rhino Mocks to assert that a method was called (and ideally with a particular parameter). The Method is ILog.Debug(FormatMessageHandler) in Common.Logging 2.0 using the new lamba syntax. It works fine using the old way plain ILog.Debug(string).
// Sample Code to Test
public int TestFuncLambda(ILog log, int a, int b)
{
log.Debug(m => m("TestFunc START"));
int c = a + b;
log.Debug(m => m("TestFunc END"));
return c;
}
public int TestFunc(ILog log, int a, int b)
{
log.Debug("TestFunc START");
int c = a + b;
log.Debug("TestFunc END");
return c;
}
[TestMethod]
public void Should_log_start_TestFuncLamba()
{
var logger = MockRepository.GenerateMock<ILog>();
logger.Stub(x => x.IsDebugEnabled).Return(true);
TestFuncLambda(logger, 1, 2);
// Doesn't work, says zero calls plus I'm not sure how to check for the word "START" in the string either
logger.AssertWasCalled(x => x.Debug(Arg<FormatMessageHandler>.Is.Anything), o => o.IgnoreArguments());
}
[TestMethod]
public void Should_log_start_TestFunc()
{
var logger = MockRepository.GenerateMock<ILog>();
logger.Stub(x => x.IsDebugEnabled).Return(true);
TestFunc(logger, 1, 2);
// Works fine
logger.AssertWasCalled(x => x.Debug(Arg<string>.Matches(Text.Contains("START"))));
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我在这里假设您只是在修改 Rhinomocks,这与日志框架无关,对吗?我这样说是因为您的测试中没有具体的实现,只有模拟。
如果不测试您的代码,这一行看起来总是为零:
因为您的实际方法 TestFunc() 将字符串传递给 log.Debug,而不是 FormatMessageHandler:
因此调用次数为零是有道理的。像这样向 TestFunc() 添加一行:
看看是否可以修复该问题。
I'm going to assume here that you are just tinkering with Rhinomocks, and this has nothing to do with the logging framework, is that correct? I say this because there are no concrete implementations in your tests, only mocks.
Without testing your code, this line looks like it will always be zero:
because your actual method TestFunc() passes strings to log.Debug, and not a FormatMessageHandler:
So it would make sense that the number of calls is zero. Add a line to TestFunc() like this:
and see if that fixes it.
首先,创建一个具体类以查看 TestFuncLambda 中是否调用了正确的 Debug() 方法。
这可以确保它不会对 lambda 到字符串进行某种奇怪的转换。
一旦您确认应该调用正确的版本,您就可以使用 RhinoMocks 隔离问题。这可能是犀牛模拟的一个错误。因此,让我们减少失败集,但在将 lambda 传递到 Debug 之前将其包装在新的 FormatMessageHandler() 中。这确保了正确的模拟函数被调用,而不是被翻译为其他函数。
如果此时您还没有发现错误,并且它仍然不起作用,请尝试创建 FormatMessageHandler() 的实例并将其保存为静态成员变量(只是为了测试问题所在)。将 TestFuncDebug 调用中保存的内容传递给 Debug() 和 AssertWasCalled() 调用。如果这不起作用,我就没主意了。
顺便说一句,我不知道 IgnoreArguments() 是什么,但我从来不需要在对 AssertWasCalled 的 RhinoMocks 调用中调用它。通常有 Arg<>.Is.Anything 就可以了。
First, create a concrete class to see if the right Debug() method is being called in TestFuncLambda.
This makes sure that it's not doing some sort of weird conversion of the lambda to string.
Once you verify that is should be calling the correct version, you've isolated the problem with RhinoMocks. It could be a bug with rhino mocks. So, lets reduce the failure set but wrapping the lambda in a new FormatMessageHandler() before you pass it into Debug. This ensures that the right mocked function is being called and not translated as something else.
If you haven't found a bug at this point, and it still doesn't work, try creating an instance of FormatMessageHandler() and saving it as a static member variable (just to test what's wrong). Pass in that saved on in the TestFuncDebug call to Debug() and the AssertWasCalled() call. If that doesn't work, I'm out of ideas.
BTW, I dont know what IgnoreArguments() is, but I never have to call it in my RhinoMocks calls to AssertWasCalled. Usually having Arg<>.Is.Anything works fine.
我想通了。我错过了代表的行动部分。正确的语法是:
而不是
如上所述,o.IgnoreArguments() 是多余的,没有必要。
I figured it out. I was missing the Action part for the delegate. The proper syntax is:
rather than
As mentioned o.IgnoreArguments() was redundant and not necessary.