Rhino Mocks 中不区分大小写的期望

发布于 2024-08-28 14:32:52 字数 424 浏览 4 评论 0原文

我正在使用 Rhino Mocks 等待电话。有一个参数,它是一个字符串。但我并不关心字符串的大小写。即使情况是错误的,我也希望测试能够通过。所以我正在做以下事情:

//expect log message to be called with a string parameter.  
//We want to ignore case when verifying so we use a constraint 
//instead of a direct parameter

Expect.Call(delegate { logger.LogMessage(null); })
      .Constraints(Is.Matching<string>(x => x.ToLower()=="f2"));

这似乎有点啰嗦。有更明智的方法吗?

I'm using Rhino Mocks to expect a call. There is a single parameter which is a string. But I'm not bothered about the case of the string. I want the test to pass even if the case is wrong. So I'm doing the following:

//expect log message to be called with a string parameter.  
//We want to ignore case when verifying so we use a constraint 
//instead of a direct parameter

Expect.Call(delegate { logger.LogMessage(null); })
      .Constraints(Is.Matching<string>(x => x.ToLower()=="f2"));

It seems a bit long-winded. Is there a more sensible way of doing this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

╰◇生如夏花灿烂 2024-09-04 14:32:52
// arrange
var loggerStub = MockRepository.GenerateStub<ILogger>();

// act
loggerStub.LogMessage("f2");

// assert
loggerStub.AssertWasCalled(
    x => x.LogMessage(Arg<string>.Matches(
        s => string.Equals(s, "f2", StringComparison.OrdinalIgnoreCase)
    ))
);

如果您不关心参数而只关心方法调用:

loggerStub.AssertWasCalled(
    x => x.LogMessage(null),
    x => x.IgnoreArguments()
);
// arrange
var loggerStub = MockRepository.GenerateStub<ILogger>();

// act
loggerStub.LogMessage("f2");

// assert
loggerStub.AssertWasCalled(
    x => x.LogMessage(Arg<string>.Matches(
        s => string.Equals(s, "f2", StringComparison.OrdinalIgnoreCase)
    ))
);

If you don't care about parameters but just the method call:

loggerStub.AssertWasCalled(
    x => x.LogMessage(null),
    x => x.IgnoreArguments()
);
荒路情人 2024-09-04 14:32:52

我会使用 @Darin 建议的 AAA 格式(或类似格式)。我认为它更简洁,但我认为您仍然必须使用相同的基本约束来进行不区分大小写的匹配。辅助方法可以使其更具可读性。

 private bool CaseInsensitive( string s, string t )
 {
      return string.Equals( s, t, StringComparison.OrdinalIgnoreCase );
 }

 var loggerMock = MockRepository.GenerateMock<Logger>();

 loggerMock.Expect( l => l.LogMessage( Arg<string>.Matches( s => CaseInsensitive( s, "f2" ))));

 classUnderTest.MethodUnderTest();

 loggerMock.VerifyAllExpectations();

I would use the AAA format that @Darin suggests (or similar). I think it's more concise, but you'll still have to use the same basic constraint for case insensitive matching, I think. A helper method can make this more readable.

 private bool CaseInsensitive( string s, string t )
 {
      return string.Equals( s, t, StringComparison.OrdinalIgnoreCase );
 }

 var loggerMock = MockRepository.GenerateMock<Logger>();

 loggerMock.Expect( l => l.LogMessage( Arg<string>.Matches( s => CaseInsensitive( s, "f2" ))));

 classUnderTest.MethodUnderTest();

 loggerMock.VerifyAllExpectations();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文