运行 Pex 探索时从 web.config 读取
我刚刚开始使用 Pex 为我的项目生成参数化单元测试。但是,当我让 Pex 运行其探索时,我的代码崩溃了,因为它无法从 web.config 中读取(更准确地说,ConfigurationSettings.AppSettings 有零个元素)。探索期间的工作目录是:“C:\Program Files (x86)\Microsoft Visual Studio 9.0\Common7\IDE”。我认为这是根本原因。
我知道处理这个问题的正确方法是创建与我需要的值相对应的模拟对象。然而,这将迫使我创建大量的模拟代码,并且不会提供任何有形的价值,恕我直言,因为我将 web.config 与测试项目捆绑在一起没有问题。
如何在 Pex 探索执行时启用从 web.config(或 app.config)读取?
I've just started using Pex to generate parameterized unit-tests for my project. However, when I let Pex run its explorations, my code crashes because it cannot read from the web.config (ConfigurationSettings.AppSettings has zero elements to be more precise). The working-directory during the explorations is: "C:\Program Files (x86)\Microsoft Visual Studio 9.0\Common7\IDE". I assume this is the root-cause.
I know that the supposedly proper way to handle this is to create mock-objects corresponding to the values I need. However, this would force me to create tons of mock-code and wouldn't provide any tangible value IMHO, because I have no problem bundling web.config with the test-project.
How do I enable reading from web.config (or app.config) while the Pex explorations executes?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
恐怕你已经回答了你自己的问题 - 你不会直接从你的代码访问你的数据库,那么为什么要使用你的配置文件呢?只需在配置文件设置周围放置一个薄包装,然后在测试中将其存根即可。您不必一次性完成所有工作,从正在测试的代码段开始,然后一点一点地将直接引用移到包装器后面。这样做的实际好处是它使测试变得容易。
另外,使用 Pex,如果您的代码在每次运行之间被完全拆除(取决于您的代码和测试是否确实如此),您每次都会访问文件系统,这将对性能产生严重影响。
You've answered your own question I'm afraid - you wouldn't directly access your database from your code, so why do it with your config files? Just put a thin wrapper around your config file settings and stub it out in your tests. You don't have to do it all in one go, start with the piece of code under test and move the direct references behind your wrapper bit by bit. The tangible benefit of doing this is that it makes testing easy.
Also, with Pex if your code is getting fully torn down between each run (depends on your code and the tests whether or not this is actually the case) you'll be hitting the file system each time which will have a serious impact on performance.
Pex 开发人员(经常)不阅读 stackoverflow。您最好在 http://social 论坛上询问与 Pex 相关的问题。 msdn.microsoft.com/Forums/en/pex/threads
The Pex developers don't read (often) stackoverflow. You better ask your Pex-related question on the forums at http://social.msdn.microsoft.com/Forums/en/pex/threads