{"action":"closed","issue":{"url":"https://api.github.com/repos/aspnet/Mvc/issues/5140","repository_url":"https://api.github.com/repos/aspnet/Mvc","labels_url":"https://api.github.com/repos/aspnet/Mvc/issues/5140/labels{/name}","comments_url":"https://api.github.com/repos/aspnet/Mvc/issues/5140/comments","events_url":"https://api.github.com/repos/aspnet/Mvc/issues/5140/events","html_url":"https://github.com/aspnet/Mvc/issues/5140","id":171026391,"number":5140,"title":"Unable to unit testing a controller on .net core 1.0","user":{"login":"rguerreiro","id":545325,"avatar_url":"https://avatars.githubusercontent.com/u/545325?v=3","gravatar_id":"","url":"https://api.github.com/users/rguerreiro","html_url":"https://github.com/rguerreiro","followers_url":"https://api.github.com/users/rguerreiro/followers","following_url":"https://api.github.com/users/rguerreiro/following{/other_user}","gists_url":"https://api.github.com/users/rguerreiro/gists{/gist_id}","starred_url":"https://api.github.com/users/rguerreiro/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rguerreiro/subscriptions","organizations_url":"https://api.github.com/users/rguerreiro/orgs","repos_url":"https://api.github.com/users/rguerreiro/repos","events_url":"https://api.github.com/users/rguerreiro/events{/privacy}","received_events_url":"https://api.github.com/users/rguerreiro/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/aspnet/Mvc/labels/question","name":"question","color":"cc317c"}],"state":"closed","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":4,"created_at":"2016-08-13T20:38:31Z","updated_at":"2016-08-19T18:27:29Z","closed_at":"2016-08-19T18:27:29Z","body":"I'm trying to unit test the following controller:\r\n```\r\nnamespace Application1.Controllers\r\n{\r\n    [Authorize]\r\n    public class HomeController : BaseController\r\n    {\r\n        private readonly IAntiforgery _antiforgery;\r\n\r\n        public HomeController(IAntiforgery antiforgery)\r\n            : base()\r\n        {\r\n            _antiforgery = antiforgery;\r\n        }\r\n\r\n        public IActionResult Index()\r\n        {\r\n            var tokens = _antiforgery.GetAndStoreTokens(HttpContext);\r\n            Response.Cookies.Append(\"XSRF-TOKEN\", tokens.RequestToken, new CookieOptions() { HttpOnly = false });\r\n\r\n            return View();\r\n        }\r\n    }\r\n}\r\n```\r\nThis is my test:\r\n```\r\nnamespace Application1.Test\r\n{\r\n    public abstract class BaseTest\r\n    {\r\n        public BaseTest()\r\n        {\r\n            var services = new ServiceCollection();\r\n\r\n            services.AddEntityFrameworkInMemoryDatabase()\r\n                .AddDbContext<ApplicationDbContext>(options => options.UseInMemoryDatabase());\r\n\r\n            InitServices(services);\r\n\r\n            ServiceProvider = services.BuildServiceProvider();\r\n\r\n            HttpContext = Substitute.For<HttpContext>();\r\n            HttpContext.Request.Returns(Substitute.For<HttpRequest>());\r\n            HttpContext.Response.Returns(Substitute.For<HttpResponse>());\r\n        }\r\n\r\n        protected virtual void InitServices(ServiceCollection services)\r\n        {\r\n            // do nothing\r\n        }\r\n\r\n        protected virtual void InitController(Controller controller)\r\n        {\r\n            controller.ControllerContext = new ControllerContext(\r\n                new ActionContext(HttpContext, new RouteData(), new ControllerActionDescriptor())\r\n            );\r\n        }\r\n\r\n        protected IServiceProvider ServiceProvider { get; set; }\r\n\r\n        protected HttpContext HttpContext { get; set; }\r\n    }\r\n\r\n    public class HomeControllerTest : BaseTest\r\n    {\r\n        protected override void InitServices(ServiceCollection services)\r\n        {\r\n            services.AddMvc();\r\n        }\r\n\r\n        [Fact]\r\n        public void Index_ReturnView()\r\n        {\r\n            // Arrange\r\n            var antiForgeryMock = Substitute.For<IAntiforgery>();\r\n            antiForgeryMock.GetAndStoreTokens(HttpContext).Returns(new AntiforgeryTokenSet(\"request token\", \"cookie token\", \"field\", \"header\"));\r\n\r\n            var controller = new HomeController(antiForgeryMock);\r\n            InitController(controller);\r\n\r\n            // Action\r\n            var result = controller.Index();\r\n\r\n            // Assert\r\n            var viewResult = result.ShouldBeOfType<ViewResult>();\r\n            viewResult.ViewName.ShouldNotBeNullOrEmpty();\r\n        }\r\n    }\r\n}\r\n```\r\n\r\nBut when in the `HomeController` when executing the line `return View();` on the `Index()` action it throws this exception:\r\n```\r\nSystem.InvalidOperationException : No service for type 'Microsoft.AspNetCore.Mvc.ViewFeatures.ITempDataDictionaryFactory' has been registered.\r\n      Stack Trace:\r\n           at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider, Type serviceType)\r\n           at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService[T](IServiceProvider provider)\r\n           at Microsoft.AspNetCore.Mvc.Controller.get_TempData()\r\n           at Microsoft.AspNetCore.Mvc.Controller.View(String viewName, Object model)\r\n```\r\n\r\nSince the [docs for unit testing the controllers](https://docs.asp.net/en/latest/mvc/controllers/testing.html) are outdated I don't have a way to check if what I'm doing is right or not.\r\n\r\n\r\n"}}
{"actor":{"display_login":"rguerreiro"}}