如何在 NUnit 中初始化 ConnectionStrings 集合

How to initialize ConnectionStrings collection in NUnit(如何在 NUnit 中初始化 ConnectionStrings 集合)
本文介绍了如何在 NUnit 中初始化 ConnectionStrings 集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 NUnit 测试 ASP.NET 应用程序,但是从 NUnit GUI 运行时,WebConfigurationManager.ConnectionStrings 集合似乎是空的.

I want to test ASP.NET application using NUnit, but it seems WebConfigurationManager.ConnectionStrings collection is empty when running from NUnit GUI.

你能告诉我如何初始化这个集合吗(可能在 [TestFixture] 的 [SetUp] 函数中)?我应该在那里复制 Web.config 吗?

Could you tell me how to initialize this collection (probably in [SetUp] function of [TestFixture])? Should I copy Web.config somethere?

谢谢!

推荐答案

如果您的单元测试程序集名为 Company.Component.Tests.dll,那么只需确保 Company.Component.Tests.dll.config 存在使用正确的连接字符串.

If you have your unit-test assembly named Company.Component.Tests.dll, then just make sure that Company.Component.Tests.dll.config is there with the proper connection string.

此外,将连接提供程序类与配置分离可能是个好主意,这样您就可以灵活地进行持久性(即:从 *.config 切换到其他内容)并更轻松地进行测试.

Additionally, it might be a good idea to decouple your connection provider class from the configuration, so that you will have flexibility in persistence (i.e.: switching from *.config to something else) and easier testing.

还可以查看NUnit 如何查找配置文件"

这篇关于如何在 NUnit 中初始化 ConnectionStrings 集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
how do i pass parameters to aspnet reportviewer(如何将参数传递给aspnet report查看器)
Bind multiple parameters from route and body to a model in ASP.NET Core(在ASP.NET Core中将路由和主体中的多个参数绑定到一个模型)
Custom model binding in AspNet Core WebApi?(AspNet Core WebApi中的自定义模型绑定?)
How to minify in .net core mvc view?(如何在.Net核心MVC视图中缩小?)