【发布时间】:2015-08-14 08:31:03
【问题描述】:
我正在使用无法更改的界面(出于遗留原因)。该接口上有采用ref 参数的方法。
在我的单元测试中,我希望在该接口上删除其中一个方法。至关重要的是,我想更改其中一个 ref 参数,但保持另一个不变。
我可以更改我想要更改的参数,但我不知道如何告诉 Rhino Mocks 让另一个保持不变。
这是一个突出问题的失败测试(请注意,实际上我在定义存根之前无权访问poco,因为它是在外部模块中实例化的):
using Rhino.Mocks;
using Rhino.Mocks.Constraints;
using NUnit.Framework;
[Test]
public void Main()
{
var serviceMock = MockRepository.GenerateMock<IService>();
serviceMock.Stub(x => x.DoSomething(
ref Arg<Poco>.Ref(Rhino.Mocks.Constraints.Is.Anything(), null).Dummy, // I don't want to specify null here
ref Arg<int>.Ref(Rhino.Mocks.Constraints.Is.Equal(1), 2).Dummy));
Poco poco = new Poco{Data = "One"};
int returnValue = 1;
serviceMock.DoSomething(ref poco, ref returnValue);
Assert.AreEqual(2, returnValue); // passes
Assert.AreEqual("One", poco.Data); // fails
}
public class Poco
{
public string Data { get; set; }
}
public interface IService
{
void DoSomething(ref Poco poco, ref int returnValue);
}
【问题讨论】:
标签: c# unit-testing mocking rhino-mocks