【发布时间】:2016-04-09 02:20:00
【问题描述】:
在下面的示例中,即使 IThing 成员 GetValue() 尚未定义,FakeItEasy 也会返回 0。我的问题是;为什么从未定义的成员调用返回值0,而不是抛出异常;是否有一些通用的模拟/伪造/存根框架软件模式规定调用未定义成员时抛出异常是禁忌?
public interface IThing
{
int GetValue();
}
public class Thing: IThing
{
public int GetValue()
{
return 1000;
}
}
[TestMethod]
public void Test1()
{
var thing= A.Fake<IThing>();
// A.CallTo(() => thing.GetValue()).Returns(1);
var val = thing.GetValue(); // Not defined but returns 0 rather than throwing an exeption
Assert.AreEqual(1, val);
}
【问题讨论】:
-
您在
var thing= A.Fake<IThing>();行中定义了您的假货,这将创建一个假的 IThing 对象(一个假的 IThing 对象,而不是一个 Thing 对象)。 0 将只是 int 的默认值(因为您没有指定它返回任何其他值)。我不确定一般的异常行为,但在这种情况下它不会抛出,因为一切都是有效的。 -
@Tone 这不是我要问的。运行时异常是在 .NET 运行时调用未定义成员时的预期行为,因为它是强类型的,我的问题是为什么 Mocking 框架具有更宽松的语义。
标签: c# unit-testing mocking stub fakeiteasy