在 RhinoMocks 中模拟返回密封类的方法

发布于 2024-07-23 21:00:43 字数 423 浏览 8 评论 0原文

运行此代码:

_foo = MockRepository.GenerateStub<IBar>();
_foo.Stub(x => x.Foo()).Return("sdf");

public interface IBar
{
   string Foo();
}

public class Bar : IBar
{
   public string Foo()
   {
      throw new NotImplementedException();
   }
}

抛出 NotSupportedException -“无法创建密封类的模拟”时。 我明白为什么不能模拟密封类(尽管 TypeMock 中有解决方案),但是模拟返回密封类(字符串)的类有什么问题?

Running this code:

_foo = MockRepository.GenerateStub<IBar>();
_foo.Stub(x => x.Foo()).Return("sdf");

When

public interface IBar
{
   string Foo();
}

public class Bar : IBar
{
   public string Foo()
   {
      throw new NotImplementedException();
   }
}

throws NotSupportedException - "Can't create mocks of sealed classes". I understand why you can't mock a sealed class (although there are solutions in TypeMock), but what's the problem with mocking a class that returns a sealed class (string) ?

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

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

发布评论

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

评论(5

我的黑色迷你裙 2024-07-30 21:00:43

Rhino Mocks 似乎正在捕获并处理此异常。 如果您启用了异常作为断点,则只能在 VS.NET 调试器中看到它。 看来您可以安全地继续运行超过此异常断点,并且一切都按预期工作。

Rhino Mocks appears to be catching and handling this exception. You only see it in the VS.NET Debugger if you've enabled exceptions as breakpoints. It appears that you can safely continue running past this exception breakpoint and it all works as expected.

沙沙粒小 2024-07-30 21:00:43

当您没有在“工具”->“选项”->“调试”->“常规”下启用“仅我的代码”,并且您有 CLR 异常,在“调试”->“异常”下选择“抛出”时,就会发生这种情况。 修复它的最简单方法是在“工具”->“选项”->“调试”->“常规”下启用“仅我的代码”。

在此处输入图像描述

This happens when you have NOT got "Just my code" enabled under Tools->Options->Debugging->General, and you have CLR exceptions, "Thrown" selected under Debug->Exceptions. Easiest way to fix it is enable "just my code" under Tools->Options->Debugging->General.

enter image description here

兔姬 2024-07-30 21:00:43

你的代码工作正常。 您可能还有一些未显示的其他代码导致了问题。 在这里发布您的整个单元测试,我们将为您诊断问题。

Your code works properly. You likely have some other code not shown which is causing the problem. Post your whole unit test here and we'll diagnose the issue for you.

小姐丶请自重 2024-07-30 21:00:43

我有同样的问题,我猜它必须是一些 VS studio 调试设置或犀牛模拟的访问权限不足。
我很确定这不是导致此问题的代码。

I have the same problem, it has to be some VS studio debug setting or some insufficient access rights for rhino mocks i guess.
I am pretty sure that its not the code that is causing this.

动听の歌 2024-07-30 21:00:43

我同意 - 这不是代码的问题。
这是VS调试设置。

我在调试下面的代码时遇到相同的异常,同时尝试将 Arg.Is.Anything 作为参数发送到存根。

mockPermissionManager.Stub(item => item.HasAccess(Arg<string>.Is.Anything)).Return(true);

异常已处理并且代码/模拟按预期工作,只需执行 F5

I second that - It's not an issue with code.
It is VS debug setting.

I get the same exception while on debug on the code below, while trying to send Arg.Is.Anything as a parameter to a stub.

mockPermissionManager.Stub(item => item.HasAccess(Arg<string>.Is.Anything)).Return(true);

The exception is handled and code/mocking works as expected, just do F5

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