我应该如何使用 nunit 在容器中测试 MVC Web 应用程序?

发布于 2024-09-19 17:38:27 字数 463 浏览 4 评论 0原文

我们有一个在 Autofac 中运行的 MVC 网络。所有配置都存储在 web.config 中的 autofac 配置部分中,当运行 Global asax 时,它会设置容器并通过向它们提供配置设置来对所有模块进行排序 - 我目前感兴趣的是NHibernate 模块 - 因此这会将连接字符串设置到其构造函数中。

我想构建一些测试来测试 NHibernate 的数据访问 - 我们有存储库和服务层,但这不一定相关。

我是否应该在包含所有测试的解决方案中添加一个额外的项目,如果我这样做,我是否需要在该测试项目的 autofac 配置中复制数据库连接字符串,并在此项目中构建一个测试容器来测试我的数据使用权?或者我应该尝试获取 Web 应用程序的容器来进行测试 - 我假设不是,因为除非被 http GET 命中,否则 Global.asax 的 Application_start 将不会运行。我真的不想复制 Web 应用程序的所有配置和容器创建,但目前我想我必须......?

We have an MVC web which is running in Autofac. All the config is stored in an autofac config section 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...?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

赴月观长安 2024-09-26 17:38:28

很抱歉看到对此反应缓慢。为实际的 DAL 编写集成测试并不常见 - 大多数时候针对存根 DAL 进行单元测试就足够了 - 因此对于这种情况可能没有太多经验。

如果您将所有配置放入 Autofac 模块中,您的 Global.asax 内容可能就像调用 builder.RegisterModule(new ConfigurationSettingsReader()) 一样简单 - 然后可以从集成中加载相同的配置文件使用采用文件名的 ConfigurationSettingsReader() 重载进行测试。

考虑使用 InstancePerLifetimeScope() 代替 HttpRequestScoped()/InstancePerHttpRequest(),而不是为 NHibernate 测试单独的模块。这具有相同的效果(除非您修改了生命周期层次结构:))并且适用于 Web 和非 Web 场景。

希望这有帮助!
缺口

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文