Rhino 和 FakeItEasy 之间的行为差​​异

发布于 2024-12-01 06:13:05 字数 1358 浏览 2 评论 0原文

我们正在考虑将我们的模拟框架从 Rhino 切换到 FakeItEasy。主要原因是简单,在 FakeItEasy 中只有一种方法可以做事。 Rhino 有记录/回放、AAA、存根、部分模拟、严格模拟、动态模拟等。

我正在使用 FakeItEasy 重写一些测试,以确保它能够完成 Rhino 目前为我们做的所有事情,并且我遇到了一些事情我无法解释,希望有人能启发我。

在Rhino中,我进行了以下测试。代码已被缩写。

ConfigurationManagerBase configManager = _mocks.Stub<ConfigurationManagerBase>();

using( _mocks.Record() )
{
    SetupResult
        .For( configManager.AppSettings["ServerVersion"] )
        .Return( "foo" );
}

附加此代码的单元测试运行良好并且测试通过。我使用 FakeItEasy 重写了它,如下所示。

ConfigurationManagerBase configManager = A.Fake<ConfigurationManagerBase>();

A.CallTo( () => configManager.AppSettings["ServerVersion"] )
    .Returns( "foo" );

现在,当我运行测试时,它失败了,但这是因为 FakeItEasy 引发了异常。

The current proxy generator can not intercept the specified method for the following reason:
  - Non virtual methods can not be intercepted.

这看起来很奇怪,因为 Rhino 也有同样的限制。我们认为正在发生的情况是,虽然 AppSettings 在 ConfigurationManagerBase 上是虚拟的,但索引器属性却不是。我们通过将 FakeItEasy 测试更改为读取来纠正该问题。

NameValueCollection collection = new NameValueCollection();
collection.Add( "ServerVersion", "foo" );

A.CallTo( () => configManager.AppSettings )
    .Returns( collection );

我基本上只是想了解我是否在使用 FakeItEasy 时做错了什么,或者 Rhino 是否在使用该索引器在幕后执行了一些“魔法”?

We're considering switching from Rhino to FakeItEasy for our mocking framework. The main reason is simplicity, in FakeItEasy there's only one way to do things. Rhino has record/playback, AAA, stub, partial mock, strict mock, dynamic mock, etc.

I'm rewriting some of our tests using FakeItEasy to ensure it will do everything Rhino is currently doing for us, and I've encountered something I can't explain and was hoping someone could enlighten me.

In Rhino, I have the following test. The code has been abbreviated.

ConfigurationManagerBase configManager = _mocks.Stub<ConfigurationManagerBase>();

using( _mocks.Record() )
{
    SetupResult
        .For( configManager.AppSettings["ServerVersion"] )
        .Return( "foo" );
}

The unit test to which this code is attached runs just fine and the test passes. I rewrote it using FakeItEasy as follows.

ConfigurationManagerBase configManager = A.Fake<ConfigurationManagerBase>();

A.CallTo( () => configManager.AppSettings["ServerVersion"] )
    .Returns( "foo" );

Now when I run the test it fails, but it's because FakeItEasy is throwing an exception.

The current proxy generator can not intercept the specified method for the following reason:
  - Non virtual methods can not be intercepted.

That seemed odd, because Rhino has the same restriction. What we think is happening in that while AppSettings is virtual on ConfigurationManagerBase, the indexer property is not. We corrected the problem by changing the FakeItEasy test to read.

NameValueCollection collection = new NameValueCollection();
collection.Add( "ServerVersion", "foo" );

A.CallTo( () => configManager.AppSettings )
    .Returns( collection );

I'm basically just trying to understand whether I'm doing something wrong with FakeItEasy or is Rhino performing some "magic" behind the scenes with that indexer?

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

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

发布评论

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

评论(1

死开点丶别碍眼 2024-12-08 06:13:05

如果这不起作用,以下配置应该与 Rhino 的配置类似 Rhino 做了一些神奇的事情:

NextCall.To(configManager.AppSettings).Returns("foo"); 
var ignored = configManager.AppSettings["ServerVersion"];

The following configuration should be similar to what Rhino does if this doesn't work Rhino does something magic:

NextCall.To(configManager.AppSettings).Returns("foo"); 
var ignored = configManager.AppSettings["ServerVersion"];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文