setup autofac and web api for unit test

Yametazamwa mara 397
Ruka hadi kwenye ujumbe wa kwanza ambao haujasomwa

b.fak...@gmail.com

hayajasomwa,
18 Mei 2015, 09:24:2718/05/2015
kwa aut...@googlegroups.com
Hi

in my web api I configure autofac in "Application_Start" like below, and it works:

var builder = new ContainerBuilder();

// Get your HttpConfiguration.
var config = GlobalConfiguration.Configuration;

builder.RegisterApiControllers(typeof(WebApiApplication).Assembly);

builder.RegisterWebApiFilterProvider(config);

builder.RegisterType<DateTimeAdapter>().As<IDateTime>();
....


var container = builder.Build();
config.DependencyResolver = new AutofacWebApiDependencyResolver(container);

But I dont know how to unit test my web api project and how to wire up things in test class. Would you help me please ?

Travis Illig

hayajasomwa,
18 Mei 2015, 09:35:4118/05/2015
kwa aut...@googlegroups.com,b.fak...@gmail.com,b.fak...@gmail.com
Unit testing a Web API controller is just like unit testing anything else. Create a new controller instance using stub dependencies, execute the controller action method, assert on what comes out.

var stubDependency = MockRepository.GenerateStub<IDependency>();
var controller = new MyController(stubDependency);
var result = controller.MyAction("some-input");
Assert.Equal("expected", result);

Are you thinking about something different? If so, perhaps some additional context in your question would help.

-T
Jibu wote
Mjibu mchapishaji
Sambaza
Ujumbe 0 mpya