热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

C#学习教程:访问方法’System.Web.Http.HttpConfiguration.DefaultFormatters()’失败分享

访问方法’System.Web.Http.HttpConfiguration.DefaultFormatters()’失败我的unittesting我的WE

访问方法’System.Web.Http.HttpConfiguration.DefaultFormatters()’失败

我的unit testing我的WEB API控制器有问题,我正在使用moq模拟我的存储库,进行设置和响应。 然后使用模拟存储库启动控制器。 问题是当我尝试从控制器执行调用时,我得到一个exception:

尝试通过方法’System.Web.Http.HttpConfiguration..ctor(System.Web.Http.HttpRouteCollection)’访问方法’System.Web.Http.HttpConfiguration.DefaultFormatters()’失败。


在System.Web.Http.HttpConfiguration..ctor(HttpRouteCollection路由)处于EyeShield.Api.Tests.PersonsControllerTests.Get_Persons_ReturnsAllPersons()的System.Web.Http.HttpConfiguration..ctor()处

说实话,不知道这里可能出现什么问题。 有谁知道这里可能存在什么问题?

控制器:

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().ToMethod(ctx => () => new Bootstrapper().Kernel); kernel.Bind().To(); 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().To(); kernel.Bind().To(); kernel.Bind().To(); } } } 

unit testing:

 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 _personService; [SetUp] public void SetUp() { _persOnService= new Mock(); } [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 GetPersonsContainers() { IEnumerable fakePersOns= new List { 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; } } } 

尝试确保安装了Microsoft.AspNet.WebApi.Client。

我的应用程序无法运行,因为我因其他原因删除了该应用程序。

打开包管理器控制台并执行:

安装包Microsoft.AspNet.WebApi.Client

确保以下Nuget打包库的版本相同:

上述就是C#学习教程:访问方法’System.Web.Http.HttpConfiguration.DefaultFormatters()’失败分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—编程笔记

 Microsoft.AspNet.WebApi Microsoft.AspNet.WebApi.Client Microsoft.AspNet.WebApi.Core Microsoft.AspNet.WebApi.WebHost 


推荐阅读
author-avatar
權yzq
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有