问题
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