在覆盖抽象方法时使用最小起订量?

问题描述:

我有这个基类

public abstract class Third : IThird
{
    public abstract ThirdUser GetUserDetails(HttpRequestBase request);
}

和这个派生类

public class LiProvider : Third
{
    public override ThirdUser GetUserDetails(HttpRequestBase request) { }
}

我试图像这样用Moq覆盖:

I tried to Moq this override like so:

mockLiProvider.Setup(x => x.GetUserDetails(It.IsAny<HttpRequestWrapper>())).Returns(user);

,但它返回null,而不是Setup中的user.

but it returns null, not the user in the Setup.

user肯定是在此测试中初始化的.

user is definitely initialised in this test.

我该如何嘲笑呢?

应该不是

mockLiProvider.Setup(x => x.GetUserDetails(It.IsAny<HttpRequestBase>())).Returns(user);

代替

mockLiProvider.Setup(x => x.GetUserDetails(It.IsAny<HttpRequestWrapper>())).Returns(user);#

请注意It.IsAny<>中的其他类型,我使用的是HttpRequestBase而不是HttpRequestWrapper.

Notice the different type in It.IsAny<>, I've used HttpRequestBase instead of HttpRequestWrapper.

您的示例没有显示如何调用它.

You example doesn't show how it is been called.

我写了这个简单的测试,就像这样:

I wrote this simple test calling it like this:

mockLiProvider.Object
.GetUserDetails(new HttpRequestWrapper(new HttpRequest("a.txt","http://a.com","")));

,并且与您的版本(HttpRequestWrapper)一起使用. 但是,如果提供了HttpRequestBase的其他派生类,则It.IsAny可能与类型不匹配.

and it works, with your version (HttpRequestWrapper). But if you are supplied some other derivative of HttpRequestBase the It.IsAny might not match the type.