开发者

How should I test an MVC web app in a container with nunit?

开发者 https://www.devze.com 2023-01-16 23:26 出处:网络
We have an MVC web which is running in Autofac.All the config is stored in an autofac config sectio开发者_如何学Cn in the web.config and when run the Global asax sets up the container and sorts out al

We have an MVC web which is running in Autofac. All the config is stored in an autofac config sectio开发者_如何学Cn in the web.config and when run the Global asax sets up the container and sorts out all the modules by providing them with their config settings - the one I'm interested in at the moment is the NHibernate module - so this gets the connection string set into its constructor.

I want to build some tests to test Data Access with NHibernate - we've got repositories and a service layer but this isn't necessarily relevant.

Should I be adding an extra project to the Solution with all my tests in and if I do this do I need to replicate the database connection string in an autofac config for this test project and build a test container in this project which will test my data access? Or should I be trying to get hold of the web app's container to do the testing with - I assume not as the Global.asax's Application_start will not run unless hit by an http GET. I don't really want to replicate all the config and container creation of the web app but at the moment I'm thinking I will have to...?


Sorry to see the slow response to this. It isn't all that common to write integration tests for the actual DAL - much of the time unit tests against a stubbed DAL are sufficient - so there's probably not as much experience out there with this kind of scenario.

If you put all of your configuration into Autofac Modules, your Global.asax content could be as simple as calling builder.RegisterModule(new ConfigurationSettingsReader()) - the same configuration file could then be loaded from your integration tests using the overload of ConfigurationSettingsReader() that takes a filename.

Rather than have a separate module for NHibernate under test, consider using InstancePerLifetimeScope() instead of HttpRequestScoped()/InstancePerHttpRequest(). This has the same effect (unless you've tinkered with the lifetime hierarchy :)) and will work in both web and non-web scenarios.

Hope this helps! Nick

0

精彩评论

暂无评论...
验证码 换一张
取 消