gpt4 book ai didi

c# - 如何 NMock FileStream?

转载 作者:太空宇宙 更新时间:2023-11-03 11:20:31 27 4
gpt4 key购买 nike

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

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 的作用吗?我不希望我的单元测试命中真正的文件系统...

最佳答案

是的,我认为您误解了 NMock 的功能。它用于创建您传递给被测对象的对象,以便它们对这些模拟对象起作用。由于您的“productionCodeObj”似乎是 FileStream 实例本身的新闻,因此它不会使用您的模拟。您必须将 file 传递给它。

关于c# - 如何 NMock FileStream?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11165418/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com