我们正在考虑为我们的模拟框架从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也有同样的限制。我们认为,虽然在ConfigurationManagerBase上AppSettings是虚拟的,但索引器属性不是。我们通过将FakeItEasy测试更改为read更正了该问题。
NameValueCollection collection = new NameValueCollection();
collection.Add( "ServerVersion", "foo" );
A.CallTo( () => configManager.AppSettings )
.Returns( collection );基本上,我只是想知道我是在用FakeItEasy做错了什么,还是Rhino在幕后用那个索引器表演了一些“魔术”?
发布于 2011-08-30 22:47:03
下面的配置应该类似于Rhino在不起作用时所做的事情Rhino做了一些神奇的事情:
NextCall.To(configManager.AppSettings).Returns("foo");
var ignored = configManager.AppSettings["ServerVersion"];https://stackoverflow.com/questions/7198256
复制相似问题