我可以将 Moq 与 MvcContrib.TestHelper 一起使用吗?

发布于 2024-11-17 12:45:16 字数 801 浏览 3 评论 0原文

我正在开发我的第一个 ASP .NET MVC 项目,在此之前我只将 Rhino.Mocks 用于桌面应用程序。

已经有一段时间了,所以我决定找出现在该领域使用的内容。
在阅读了一些赞扬 起订量,我决定接受它。
(我喜欢某些人所说的 lambda 误用和过度使用)。

然而,我也听到了对 MvcContrib TestHelper 框架的赞扬,似乎依赖于 Rhino.Mocks

我需要知道TestHelper是否强制我使用Rhino.Mocks。我不想在测试中混合使用两种不同的模拟框架,因此如果 TestHelper 是特定于 Rhino 的,我将不得不决定要么

  • 为了 TestHelper 而切换到 Rhino.Mocks(悲伤但有可能);
  • 为了使用 Moq 而放弃 TestHelper(不太可能)。

我真的希望 TestHelper 在其公共 API 中独立于 Rhino.Mocks,但我不能 100% 确定。
有人可以(反)确认吗?

I'm working on my first ASP .NET MVC project and prior to this moment I've only used Rhino.Mocks for desktop applications.

It's been a while so I decided to find out what's used in the field now.
After reading some praise for Moq, I decided to go with it.
(I love what some may call lambda mis- and overuse).

However I also heard praise for MvcContrib TestHelper framework which seems to have a dependency on Rhino.Mocks.

I need to know whether TestHelper imposes using Rhino.Mocks on me. I wouldn't want to mix two different mocking frameworks in my tests so in case TestHelper is Rhino-specific, I'll have to decide either to

  • switch to Rhino.Mocks for the sake of TestHelper (sad but likely);
  • abandon TestHelper for the sake of using Moq (unlikely).

I really hope TestHelper is independent of Rhino.Mocks in its public APIs but I'm not 100% sure.
Can someone (dis)confirm it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

灯下孤影 2024-11-24 12:45:16

我不确定何时更改,但 TestControllerBuilder 现在采用可选的 IMockFactory 参数。我发现的问题是,如果你不传入一个,它首先默认为 RhinoMocks,然后默认为 Moq,两者都作为代理。如果您像我一样使用 StructureMap,它将始终使用 RhinoMocks。我真的希望有某种标志来告诉 TestControllerBuilder 使用哪一个。

要强制构建器使用 Moq,您可以传入一个新的 IMockFactory 类,该类会反射回 Moq。在内部,MvcContrib 有一个类可以完美地完成此操作,但它不能公开访问。我不知道他们为什么不曝光它。

我没有创建自己的 IMockFactory 实现,而是选择深入研究 MvcContrib 代码并复制/粘贴 MoqFactory 和 <一个href="http://mvccontrib.codeplex.com/SourceControl/changeset/view/1129ee553134#src/MvcContrib.TestHelper/MvcContrib.TestHelper/MockFactories/MoqProxy.cs" rel="nofollow">MoqProxy 类到我自己的解决方案并将它们传递到 TestControllerBuilder 中。它感觉真的很脏,但它完成了工作,让我可以随心所欲地使用 Moq。

I'm not sure when this was changed, but the TestControllerBuilder now takes an optional IMockFactory<T> argument. The problem I found with this is that if you don't pass one in, it defaults first to RhinoMocks and then to Moq, both as proxies. If you're using StructureMap, as I am, it will always use RhinoMocks. I really wish there were a flag of some sort to tell the TestControllerBuilder which one to use.

To force the builder to use Moq, you can pass in a new IMockFactory<T> class that reflects back into Moq. Internally, MvcContrib has a class that does this perfectly, but it is not publicly accessible. I'm not sure why they didn't expose it.

Instead of creating my own implementation of IMockFactory<T>, I chose to dig into the MvcContrib code and copy/pasta the MoqFactory and MoqProxy classes into my own solution and pass them into the TestControllerBuilder. It feels really dirty, but it gets the job done and lets me use Moq to my heart's content.

放肆 2024-11-24 12:45:16

我需要知道 TestHelper 是否强制我使用 Rhino.Mocks

的,它确实强制你使用 Rhino Mocks。 TestHelperBuilder 类上的所有属性(例如 Request、Response、Session、HttpContext 等)都使用 Rhino Mocks 进行存根,并等待您对它们编写期望。

I need to know whether TestHelper imposes using Rhino.Mocks on me

Yes, it does impose you to use Rhino Mocks. All the properties such as Request, Response, Session, HttpContext, ... on the TestHelperBuilder class are stubbed with Rhino Mocks and are waiting for you to write expectations on them.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文