C# 可以同时执行 WebForms 和 MVC 的最佳模拟框架?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10098/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Best mock framework that can do both WebForms and MVC?
提问by DevelopingChris
I'm getting into more of a TDD workflow, and have a mix of MVC and asp.net Web Forms apps.
我正在研究更多的 TDD 工作流,并且混合使用 MVC 和 asp.net Web 窗体应用程序。
MOQ is recommended for MVC.
MVC 建议使用 MOQ。
I've used Rhino for Web Forms.
我已经将 Rhino 用于 Web 表单。
Does anyone have a best practice for having 1 framework mock for both?
有没有人有同时拥有 1 个框架模拟的最佳实践?
采纳答案by Rory MacLeod
Rhino's latest release includes much of the sweet sweet 3.5 love that MoQ has. I'm a fan of MoQ, so that's what I'm using. But I also have Rhino, in case it does something that MoQ doesn't do.
Rhino 的最新版本包括 MoQ 所拥有的许多甜蜜的 3.5 爱。我是 MoQ 的粉丝,所以这就是我正在使用的。但我也有 Rhino,以防它做了一些 MoQ 没有做的事情。
TL;DR: MoQ it baby.
TL; DR:MoQ it baby。
回答by Rory MacLeod
I would just go ahead and use my favourite framework for both. I don't think there's any reason that I would choose one framework for web forms and another for MVC. A far bigger problem is how I would unit test my web forms pages at all, since it's notoriously hard to seperate the page from the rest of the HttpRequest stack.
我会继续使用我最喜欢的框架。我认为没有任何理由为 Web 表单选择一种框架而为 MVC 选择另一种框架。一个更大的问题是我将如何对我的 Web 表单页面进行单元测试,因为众所周知很难将页面与 HttpRequest 堆栈的其余部分分开。
My favourite is Moq. I've also used TypeMock. It costs money, but it's really powerful - it lets you mock concrete classes and constructors, so you could potentially mock things like HttpContext or HttpRequest.
我最喜欢的是起订量。我也使用过 TypeMock。它要花钱,但它真的很强大——它可以让你模拟具体的类和构造函数,所以你可以模拟 HttpContext 或 HttpRequest 之类的东西。
回答by CVertex
TypeMock is insanely powerful. When I neededto unit test a web forms app that wasn't designed for testability TypeMock saved my life.
TypeMock 非常强大。当我需要对一个不是为可测试性而设计的 Web 表单应用程序进行单元测试时,TypeMock 挽救了我的生命。
But when I take the time to pick an architectural pattern (MVC) or design one that allows for Mockability (you know, public virtualize state changing methods) I use Moq. It is so simple to use and so simple to teach others.
但是,当我花时间选择一种架构模式 (MVC) 或设计一种允许 Mockability(您知道,公共虚拟化状态更改方法)的架构模式时,我会使用 Moq。使用起来如此简单,教别人也如此简单。
TypeMock's record replay syntax still confuses me, but it saved me plenty of time in a tight release schedule. Moq's API is almost self explanatory which is an amazing achievement given the mock library history.
TypeMock 的记录重放语法仍然让我感到困惑,但它在紧张的发布时间表中为我节省了大量时间。Moq 的 API 几乎是不言自明的,考虑到模拟库的历史,这是一项了不起的成就。
回答by Matt Hinze
This is sort of a silly question, but I prefer Rhino Mocks as it represents a more complete understanding of mocks vs. stubs.
这是一个愚蠢的问题,但我更喜欢 Rhino Mocks,因为它代表了对mocks 与 stubs的更完整的理解。
Look deep into TypeMock before committing to the price.
在确定价格之前深入研究 TypeMock。
Also, there is no recommended mocking framework for ASP.NET MVC.
此外,没有推荐的 ASP.NET MVC 模拟框架。
Finally - I'd suggest you stick to one mocking framework in your project (and even in your team) - the differences, while not huge, can lead to confusion that is unwarranted on such a "polishing-the-rock" decision. By that I mean the decision should not be a long one, just pick what works and get on with creating value.
最后——我建议你在你的项目中(甚至在你的团队中)坚持使用一个模拟框架——这些差异虽然不是很大,但可能会导致混淆,这对这种“磨砺”的决定是没有根据的。我的意思是决定不应该是一个很长的决定,只要选择有效的方法并继续创造价值即可。
回答by RoyOsherove
Look into Ivonna for faking HTTPContext and traditional webforms.
调查 Ivonna 伪造 HTTPContext 和传统网络表单。