如何进行 Web 测试的模拟?
我想为我的 asp.net Web 应用程序编写一些 Web 测试(通过 WatiN/Selenium + CassiniDev Web 服务器)。
我遇到的问题是我不知道在这种情况下该怎么办: 有一个页面,用户可以单击按钮来调用某些第三方服务。在我的网络测试中,我想创建此服务的模拟,它将始终返回静态值(这些测试用例中的某些值和其他测试用例中的其他值)。
我怎样才能做到这一点?
目前我使用 IoC/DI 容器 Microsoft Unity。我的页面以 中描述的方式获取他的依赖项http://msdn.microsoft.com/en-us/library/ff664622%28v=pandp.50%29.aspx。
我想到的唯一解决方案是:将每个测试用例的所有依赖项都放在 web.config 中,并在测试的 SetUp 上复制必要的 web.config。这个解决方案非常痛苦!
有什么想法吗?
I want to write a few web tests (over WatiN/Selenium + CassiniDev web server) for my asp.net web application.
Problem I encountered is that I dont know what to do in such situations:
there is a page where user can click the button to call some third-party service. In my web test i want to create mock of this service, which will always return static value (some value in these test case and other value in other test case).
How can i do that?
Currently i use IoC/DI container Microsoft Unity. And my pages gets his dependencies in a manner described in http://msdn.microsoft.com/en-us/library/ff664622%28v=pandp.50%29.aspx.
The only solution that comes to my head is: place all dependencies in web.config for each test case and copy necessary web.config on SetUp of test. This solution completly painful!
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我在集成测试中也使用 WatiN 和 Cassini-dev,并且不得不处理类似的问题。在我的安装装置中,我将 Asp.Net Web 应用程序部署到测试文件夹中的临时文件夹,这使我可以在启动 cassini-dev 之前尝试配置。我在 CI 中使用 Windsor,它允许我在配置级别更改注入的组件。您也可以使用 Unity 来实现此目的。
如果您引用的服务是 Web 服务,您只需使用您已编码的接口模拟 Web 服务即可。
以下是我运行集成测试时采取的步骤:
运行测试。
运行测试后,您应该进行清理。
如何使用 MSbuild 部署网站代码
Unity 配置节用法: http://www.pnpguidance.net/Post/UnityContainerUnityConfigurationSectionAppConfigWebConfig.aspx< /a>
在代码中生成 asp.net 会员数据库: http: //bronumski.blogspot.com/2011/06/generating-creating-aspnet-application.html
MSDN 上的 Msbuild ProjectCollection:http://msdn.microsoft.com/en-us/library/microsoft.build.evaluation.projectcollection.aspx
I use WatiN and Cassini-dev in my integration tests as well and have had to deal with similar issues. In my setup fixture I deploy my Asp.Net web application to a temporary folder in my test folder which allows me to play around with the configuration before starting up cassini-dev. I use Windsor for my CI which allows me to change injected components at the configuration level. You may also be able to acheive this with Unity.
If the service you are referring to is a web service you just mock out a web service using the interface you have been coding to.
Here are the steps that I take when running my integration tests:
Run the tests.
After running the tests you should clean up.
How to deploy a website using MSbuild in code
Unity configuration section usage: http://www.pnpguidance.net/Post/UnityContainerUnityConfigurationSectionAppConfigWebConfig.aspx
Generating asp.net membership database in code: http://bronumski.blogspot.com/2011/06/generating-creating-aspnet-application.html
Msbuild ProjectCollection on MSDN: http://msdn.microsoft.com/en-us/library/microsoft.build.evaluation.projectcollection.aspx
听起来您正在尝试模拟网络服务。
Web 服务通常继承自 MarshalByRefObject,这意味着您可以通过继承 RealProxy 来创建模拟,以创建一个假装是 Web 服务的透明代理:
我相信 NMock2 使用 RealProxy 进行模拟,因此您应该能够使用它来模拟而是使用网络服务。
It sounds like you are trying to mock a web service.
Web services usually inherit from MarshalByRefObject, this means you can create a mock by inheriting from RealProxy to create a transparent proxy that pretends to be the webservice:
I belieave NMock2 uses RealProxy for it's mocks, so you should be able to use it to mock the web service instead.