【发布时间】:2010-12-25 06:23:01
【问题描述】:
我尝试使用 NMock2,但在尝试将模拟传递给构造函数时遇到 TypeLoadExceptions,我还看到 TypeMock 可以做到这一点,但它要花费 80 美元
【问题讨论】:
标签: unit-testing parameters mocking
我尝试使用 NMock2,但在尝试将模拟传递给构造函数时遇到 TypeLoadExceptions,我还看到 TypeMock 可以做到这一点,但它要花费 80 美元
【问题讨论】:
标签: unit-testing parameters mocking
我自己发现,你实际上可以用 Moq 做到这一点,就像这样:
var info = new Info { stuff = 1 };
textReader.Setup(o => o.Read<CandidateCsv>("", out info));
就是这样:)
【讨论】:
Moq does not 支持模拟 out/ref 参数,但您可以使用带有 OutRef 的 Rhino Mocks 来做到这一点,它为方法中的每个 out/ref 参数接受一个参数。
MockRepository mockRepository = new MockRepository();
// IService.Execute(out int result);
var mock = mockRepository.CreateStub<IService>();
int mockResult; // Still needed in order for Execute to compile
mock.Setup(x => x.Execute(out mockResult)).OutRef(5);
mock.Replay();
int result;
mock.Execute(out result);
Assert.AreEqual(5, result);
【讨论】:
ref 参数的方法调用,然后指定将该参数更改为另一个值。
ref 参数,出于所有目的,不受支持。不过,out 参数是受支持的。