我無法使用Rhino Mocks來聲明一個方法被調用(並且理想地使用特定參數)。方法是使用新的lamba語法在Common.Logging 2.0中的ILog.Debug(FormatMessageHandler)。它工作正常使用舊的方式普通ILog.Debug(字符串)。Rhino Mocks - 在common.Logging上使用AssertWasCalled ILog.Debug
// 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"))));
}
我在我剛剛修復的示例中有一些拼寫錯誤。是的,這只是一個例子的簡單測試。 TestFunc的工作原理,TestFuncLamba沒有。希望現在更清楚。 – ss2k 2009-12-22 03:31:55