我的单元测试我的WEB API控制器有问题,我正在使用moq模拟我的存储库,进行设置和响应。然后使用模拟存储库启动控制器。问题是当我尝试从控制器执行调用时,我得到一个异常:
尝试方法 'System.Web.Http.HttpConfiguration..ctor(System.Web.Http.HttpRouteCollection)' 访问方法 'System.Web.Http.HttpConfiguration.DefaultFormatters()'失败。
在System.Web.Http.HttpConfiguration..ctor(HttpRouteCollection) 在System.Web.Http.HttpConfiguration..ctor()的at) EyeShield.Api.Tests.PersonsControllerTests.Get_Persons_ReturnsAllPersons()
说实话,不知道这里可能出现什么问题。有人知道这里可能存在什么问题吗?
控制器:
using System;
using System.Net;
using System.Net.Http;
using EyeShield.Api.DtoMappers;
using EyeShield.Api.Models;
using EyeShield.Service;
using System.Web.Http;
namespace EyeShield.Api.Controllers
{
public class PersonsController : ApiController
{
private readonly IPersonService _personService;
public PersonsController(IPersonService personService)
{
_personService = personService;
}
public HttpResponseMessage Get()
{
try
{
var persons = PersonMapper.ToDto(_personService.GetPersons());
var response = Request.CreateResponse(HttpStatusCode.OK, persons);
return response;
}
catch (Exception e)
{
return Request.CreateErrorResponse(HttpStatusCode.BadRequest, e.Message);
}
}
}
}
的Global.asax:
using EyeShield.Data.Infrastructure;
using EyeShield.Data.Repositories;
using EyeShield.Service;
using Ninject;
using Ninject.Web.Common;
using System;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Routing;
namespace EyeShield.Api
{
public class MvcApplication : NinjectHttpApplication
{
protected override void OnApplicationStarted()
{
base.OnApplicationStarted();
AreaRegistration.RegisterAllAreas();
WebApiConfig.Register(GlobalConfiguration.Configuration);
WebApiConfig.ConfigureCamelCaseResponse(GlobalConfiguration.Configuration);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
}
protected override IKernel CreateKernel()
{
var kernel = new StandardKernel();
kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel);
kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();
RegisterServices(kernel);
// Install our Ninject-based IDependencyResolver into the Web API config
GlobalConfiguration.Configuration.DependencyResolver = new NinjectDependencyResolver(kernel);
return kernel;
}
private void RegisterServices(IKernel kernel)
{
// This is where we tell Ninject how to resolve service requests
kernel.Bind<IDatabaseFactory>().To<DatabaseFactory>();
kernel.Bind<IPersonService>().To<PersonService>();
kernel.Bind<IPersonRepository>().To<PersonRepository>();
}
}
}
单元测试:
using System.Collections.Generic;
using EyeShield.Api.Controllers;
using EyeShield.Api.DtoMappers;
using EyeShield.Api.Models;
using EyeShield.Service;
using Moq;
using NUnit.Framework;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Http.Hosting;
namespace EyeShield.Api.Tests
{
[TestFixture]
public class PersonsControllerTests
{
private Mock<IPersonService> _personService;
[SetUp]
public void SetUp()
{
_personService = new Mock<IPersonService>();
}
[Test]
public void Get_Persons_ReturnsAllPersons()
{
// Arrange
var fakePesons = GetPersonsContainers();
_personService.Setup(x => x.GetPersons()).Returns(PersonMapper.FromDto(fakePesons));
// here exception occurs
var controller = new PersonsController(_personService.Object)
{
Request = new HttpRequestMessage()
{
Properties = { { HttpPropertyKeys.HttpConfigurationKey, new HttpConfiguration() } }
}
};
// Act
var response = controller.Get();
string str = response.Content.ReadAsStringAsync().Result;
// Assert
Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
}
private static IEnumerable<PersonContainer> GetPersonsContainers()
{
IEnumerable<PersonContainer> fakePersons = new List<PersonContainer>
{
new PersonContainer {Id = 1, Name = "Loke", Surname = "Lamora", PersonalId = "QWE654789", Position = "Software Engineer"},
new PersonContainer {Id = 2, Name = "Jean", Surname = "Tannen", PersonalId = "XYZ123456", Position = "Biology Lab Assistant"},
new PersonContainer {Id = 3, Name = "Edward", Surname = "Crowley", PersonalId = "ABC654789", Position = "System Infrastructure"}
};
return fakePersons;
}
}
}
答案 0 :(得分:14)
尝试确保安装了Microsoft.AspNet.WebApi.Client。
我的应用无法正常工作,因为我已将其删除了。
打开软件包管理器控制台并执行:
安装包Microsoft.AspNet.WebApi.Client
答案 1 :(得分:7)
确保以下Nuget打包库的版本相同:
Microsoft.AspNet.WebApi
Microsoft.AspNet.WebApi.Client
Microsoft.AspNet.WebApi.Core
Microsoft.AspNet.WebApi.WebHost
答案 2 :(得分:1)
检查您的packages.config,确保以下Nuget打包库具有相同版本:
<package id="Microsoft.AspNet.WebApi" version="x.x.x" targetFramework="net45" />
<package id="Microsoft.AspNet.WebApi.Client" version="x.x.x" targetFramework="net45" />
<package id="Microsoft.AspNet.WebApi.Core" version="x.x.x" targetFramework="net45" />
<package id="Microsoft.AspNet.WebApi.WebHost" version="x.x.x" targetFramework="net45" />
从WebApi,WebApiWebhost,WebApi.Core,WebApi.Client卸载软件包
PM> uninstall-package Microsoft.AspNet.WebApi
PM> uninstall-package Microsoft.AspNet.WebApi.WebHost
PM> uninstall-package Microsoft.AspNet.WebApi.Core
PM> uninstall-package Microsoft.AspNet.WebApi.Client
重新安装程序包
PM> install-package Microsoft.AspNet.WebApi -version x.x.x