2

我的单元测试中有这段代码:

var file = m_Mockery.NewMock<IFile>();
Stream s = new MemoryStream();

Expect.Once.On( file ).Method( "OpenRead" )
.With( "someFile.mdb")
.Will( Return.Value( s ) );
...
...
...

// this runs the real code that contains the OpenRead call
productionCodeObj.DoIt("someFile.mdb");
m_Mockery.VerifyAllExpectationsHaveBeenMet();

问题是当我调用 DoIt(它调用 OpenRead)时,我得到一个异常,说找不到文件。我是否误解了 nmock 的作用?我不希望我的单元测试碰到真正的文件系统......

4

1 回答 1

2

是的,我认为您误解了 NMock 的功能。它用于创建传递给被测对象的对象,以便它们作用于那些模拟对象。似乎您的“productionCodeObj”会通知一个FileStream实例本身,因此它不会使用您的模拟。你必须file进入它。

于 2012-06-23T03:04:30.187 回答