我需要在使用odata的web api中对控制器进行单元测试。
我使用Moq来模拟实体框架。我无法模拟OdataQueryOptions作为参数传递给控制器的动作。
[TestMethod]
public void TestMethod1()
{
var data = new List<ActivationRequestLog>
{
new ActivationRequestLog { ActivationRequestLogID=1, SourceTokenType="happy", ProductKeyID=20140265000001, ActivationStatusCode="", AttemptDateOffset=null, HWID="", LogCreatedDateOffset=null },
new ActivationRequestLog { ActivationRequestLogID=1, SourceTokenType="happy", ProductKeyID=20140265000001, ActivationStatusCode="", AttemptDateOffset=null, HWID="", LogCreatedDateOffset=null },
new ActivationRequestLog { ActivationRequestLogID=1, SourceTokenType="happy", ProductKeyID=20140265000001, ActivationStatusCode="", AttemptDateOffset=null, HWID="", LogCreatedDateOffset=null },
}.AsQueryable();
var mockSet = new Mock<DbSet<ActivationRequestLog>>();
mockSet.As<IQueryable<ActivationRequestLog>>().Setup(m => m.Provider).Returns(data.Provider);
mockSet.As<IQueryable<ActivationRequestLog>>().Setup(m => m.Expression).Returns(data.Expression);
mockSet.As<IQueryable<ActivationRequestLog>>().Setup(m => m.ElementType).Returns(data.ElementType);
mockSet.As<IQueryable<ActivationRequestLog>>().Setup(m => m.GetEnumerator()).Returns(data.GetEnumerator());
var mockContext = new Mock<AVSDataAccessViewEntities>();
mockContext.Setup(c => c.ActivationRequestLogs).Returns(mockSet.Object);
var request = new HttpRequestMessage(HttpMethod.Get, "http://localhost/someurl");
//var opts = new ODataQueryOptions<ActivationRequestLog>(new ODataQueryContext(modelBuilder.GetEdmModel(), typeof(ActivationRequestLog),path), request);
var service = new AVSController(mockContext.Object);
var activationRequestLog = service.GetActivationRequestLog(opts);
Assert.AreEqual(2, activationRequestLog.Count());
}
}
}
我需要找到一种初始化变量 opts
的合适方法答案 0 :(得分:1)
odata的测试会帮助你吗?
ODataQueryContext context = new ODataQueryContext(EdmCoreModel.Instance, typeof(int));
HttpRequestMessage request = new HttpRequestMessage();
request.EnableHttpDependencyInjectionSupport();
Mock<ODataQueryOptions> queryOptions = new Mock<ODataQueryOptions>(context, request);