我有一个ASP.NET MVC4 Web API项目,其中包含一个ApiController继承控制器,它接受一个ODataQueryOptions参数作为其输入之一.
我正在使用NUnit和Moq来测试项目,这允许我从ApiController使用的相关存储库方法设置预设响应.这有效,如:
[TestFixture]
public class ProjectControllerTests
{
[Test]
public async Task GetById()
{
var repo = new Mock<IManagementQuery>();
repo.Setup(a => a.GetProjectById(2)).Returns(Task.FromResult<Project>(new Project()
{
ProjectID = 2, ProjectName = "Test project", ProjectClient = 3
}));
var controller = new ProjectController(repo.Object);
var response = await controller.Get(2);
Assert.AreEqual(response.id, 2);
Assert.AreEqual(response.name, "Test project");
Assert.AreEqual(response.clientId, 3);
}
}
Run Code Online (Sandbox Code Playgroud)
我面临的挑战是,要使用此模式,我需要将相关的查询字符串参数传递给控制器以及存储库(这实际上是我的意图).但是,在ODataQueryOptions接受ApiController方法的情况下,即使在我只想使用ODataQueryOptions的默认参数的情况下,我也需要知道如何实例化一个.这变得棘手:
我需要做什么/有更好的方法吗?
谢谢.
我正在尝试对OData控制器进行单元测试,但API已更改,之前推荐的方法我尝试不起作用 - 目前我正在使用
没有注册非OData HTTP路由.
当试图将ODataQueryOptions实例化为传递给控制器的Get方法时
我当前的代码(基于像回答这一个):
[TestMethod()]
public void RankingTest()
{
var serviceMock = new Mock<IVendorService>();
serviceMock.SetReturnsDefault<IEnumerable<Vendor>>(new List<Vendor>()
{
new Vendor() { id = "1" }
});
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "http://localhost/odata/Vendor");
ODataModelBuilder builder = new ODataConventionModelBuilder();
builder.EntitySet<Vendor>("Vendor");
var model = builder.GetEdmModel();
HttpRouteCollection routes = new HttpRouteCollection();
HttpConfiguration config = new HttpConfiguration(routes) { IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always };
// attempting to register at least some non-OData HTTP route doesn't seem to help
routes.MapHttpRoute("Default", "{controller}/{action}/{id}",
new
{
controller …Run Code Online (Sandbox Code Playgroud)