在覆盖抽象方法时使用最小起订量? [英] Using moq on an override of abstract method?

查看:71
本文介绍了在覆盖抽象方法时使用最小起订量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个基类

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.

这篇关于在覆盖抽象方法时使用最小起订量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆