我们正在考虑将我们的模拟框架从 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 上是虚拟的,但 indexer 属性却不是。我们通过将 FakeItEasy 测试更改为 read 来纠正问题。
NameValueCollection collection = new NameValueCollection();
collection.Add( "ServerVersion", "foo" );
A.CallTo( () => configManager.AppSettings )
.Returns( collection );
我基本上只是想了解我是否对 FakeItEasy 做错了,或者 Rhino 是否在使用该索引器在幕后执行了一些“魔术”?