带有 Web 配置的 TDD
所有伟大的故事总是以这 4 个神奇的词开始......我继承了一个系统......不等等!这是不对的!
不管怎样,我的幽默尝试现在已经过去了,我没有得到更多的支持,我必须支持现有的服务。
使用此服务时会遇到很多问题,例如创建一个人的记录,您需要调用服务的 4 个不同部分。
因此,与我的经理一起,我们决定需要在顶部添加另一层来为常见请求添加外观,以简化创建新站点时的数量和执行这些操作的正确顺序。
如果有人想避免上述华夫饼,我的问题就从这里开始
所以我想在我正在做的工作中使用TDD,但是我继承的服务(将成为我们的数据层)已经强耦合了数据库连接字符串位于 Web.Config 中的特定 connetionstring 节点中。
我遇到的问题是,将服务与配置文件分离将花费我几周的时间,而我没有时间。
因此,我必须将带有预期节点的 App.Config 文件添加到我的测试项目中。
这样做可以吗,还是我应该开始投入一些时间将数据库配置与数据层解耦?
All great stories they always start with those 4 magical words... I have inherited a system... no wait! that isn't right!
Anyway with my attempt at humour now passed I have not so much been given more I have to support an existing service.
There are many many issues when it comes to using this service, as an example one is to create a record of a person, you need to call 4 different parts of the services.
So, getting together with my Manager we decided that we need to stick another layer on top to add a facade to the common requests, to simplify the number things and the correct order to do them when creating a new site.
My question starts here if anyone wants to avoid the above waffle
So I want to use TDD on the work I am doing, but the service I have inherited (which will become our Data layer) has been strongly coupled with a database connection string located in a specific connetionstring node in the Web.Config.
Problem I have is, to decouple the service from the Config file will take weeks of my time which I do not have.
So I have had to add and App.Config file with the expected node into my Test project.
Is it ok to do this, or should I start investing some time to decouple the database config from the datalayer?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我同意您可能应该考虑使用依赖注入来通过代码将应用程序与配置解耦,但是,我也明白这样做并不是一件容易的事。
因此,直接回答您的问题,不,添加配置文件来支持您的测试没有任何问题。这对于对遗留系统进行单元测试实际上很常见(遗留系统是未经测试的系统)。当没有其他选择时,我还诉诸利用反射将假配置值“注入”到 ConfigurationManager 中,以测试正在读取配置值的代码,但这可能是最后的手段。
I agree that you should probably look into using Dependency Injection as your working your way through the code to decouple your app from the config, however, I also understand that doing that is not going to be an easy task.
So, to answer your question directly, no, there is nothing wrong with adding a config file to support your tests. This is actually quite common for unit testing legacy systems (legacy being an un-tested system). I have also, when left with no other option, resorted to utilizing reflection to "inject" fake configuration values into the ConfigurationManager in order to test code that is reading configuration values, but this is probably a last resort.
尝试使用依赖注入来模拟您的数据层。
在 TDD 中,您(不一定)不测试数据层和数据库,而是测试业务逻辑。
一些链接:
Try using Dependancy Injection to mock up your DataLayer.
In TDD you are not (necessarily) testing your datalayer and database but the BusinessLogic.
Some links:
您可以使用依赖注入从 web.config(或 app.config)中“解开”您的代码:
http://weblogs.asp.net/psteele/archive/2009/11/23/use-dependency-injection-to -简化应用程序设置.aspx
You can use Dependency Injection to "untie" your code from web.config (or app.config for that matter):
http://weblogs.asp.net/psteele/archive/2009/11/23/use-dependency-injection-to-simplify-application-settings.aspx
正如您提到的,依赖注入是可行的方法。您还希望确保配置对象的使用者不依赖于您的特定配置实现,例如 ConfigurationManager、ConfigurationSections 等。要查看使用自定义配置的完整示例,您可以查看我的博客文章 配置忽略 但基本上它包括。
无论是使用 ConfigurationSection 还是 XmlReader,您的配置实现都应该基于一个接口,这样您就可以轻松模拟属性并在以后轻松更改实现。
为了解决如何重试配置的问题,我们使用配置提供程序,特定配置的配置提供程序知道如何检索其配置。
如果您使用 Windsor,则可以将其连接到 Windsor 的工厂设施。
希望有帮助。
As you mentioned Dependency Injection is the way to go. You also want to make sure that your consumers of your configuration object are not dependent on your specific configuration implementation such as the ConfigurationManager, ConfigurationSections, etc. To see a full example using custom configuration you can have a look at my blog post on Configuration Ignorance but basically it comprises of.
Your configuration implementation be it using a ConfigurationSection or an XmlReader should be based on an interface that way you can easily mock out your properties and easily change your implementation at a later date.
To tackle how the the configuration is retried we use a configuration provider, the configuration provider for a particular configuration knows how to retrieve it's configuration
If you are using Windsor you can then wire this up to Windsor's factory facility.
Hope that helps.