I have method:
public override someClass getX(HttpRequestBase request){ ... }
now, I want to mock it.
I tried
mockProvider.Setup(x => x.getX(It.IsAny<HttpRequestWrapper>())).Returns(someClassInstance);
but it return null, not someClassInstance (by debug i can see it's not null). what can i do? thanks!
I don't exactly understand why you expecte the result to be "2" nor can i see the declaration of someClassInstance
to verify if the Assert should or should not be NULL.
However, I implemented those Methods and wrapped a test around:
using System.Web;
using Moq;
using NUnit.Framework;
public class FooBase
{
public virtual ResultObject getX(HttpRequestBase request)
{
return new ResultObject { Id = 2 };
}
}
public class Foo : FooBase
{
public override ResultObject getX(HttpRequestBase request)
{
return new ResultObject { Id = 4 };
}
}
public class ResultObject
{
public int Id { get; set; }
}
[TestFixture]
public class Test
{
Mock<Foo> mockProvider = new Mock<Foo>();
[Test]
public void FooTest()
{
// Arrange
var fakedResultObject = new ResultObject { Id = 8 };
mockProvider.Setup(x => x.getX(It.IsAny<HttpRequestWrapper>())).Returns(fakedResultObject);
// Act
var result = mockProvider.Object.getX(new HttpRequestWrapper(new HttpRequest("filename", "http://foo.org", "querystring")));
//Assert
Assert.AreEqual(8, result.Id);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.