Using moq on an override of abstract method?

时光毁灭记忆、已成空白 提交于 2019-12-25 03:52:51

问题


I have this base class

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

and this derived class

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

I tried to Moq this override like so:

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

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

user is definitely initialised in this test.

How can I mock this?


回答1:


Shouldn't it be

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

instead of

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

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","")));

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.



来源:https://stackoverflow.com/questions/33532536/using-moq-on-an-override-of-abstract-method

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!