setup autofac and web api for unit test

已查看 397 次
跳至第一个未读帖子

b.fak...@gmail.com

未读,
2015年5月18日 09:24:272015/5/18
收件人 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

未读,
2015年5月18日 09:35:412015/5/18
收件人 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
回复全部
回复作者
转发
0 个新帖子