0
我正在嘗試做一個簡單的測試,嘲笑sum方法。.NET MOQ返回不同的結果
我有一個接口:
public interface ISumSomething
{
int Sum(params int[] values);
}
使用該接口的類:
public class CallSum
{
public CallSum(ISumSomething sumSomething)
{
this.SumSomething = sumSomething;
}
private ISumSomething SumSomething { get; set; }
public int Execute(params int[] values)
{
return this.SumSomething.Sum(values);
}
}
,並且測試類:
[TestMethod]
public void Test_Sum_Method()
{
// Creates MOQ.
var instance = new Mock<ISumSomething>();
// Setup de MOQ.
instance.Setup(moq => moq.Sum(It.IsAny(1,2)).Returns(4));
// Instance the object.
var sum = new CallSum(instance.Object);
// Execute the operation.
var result = sum.Execute(2, 2);
// Check the result.
Assert.AreEqual(4, result);
}
的問題是,當我打電話執行方法,它正在退回0
,但在我的最小起訂量中,我正在設置4
。爲什麼會發生?
這是'Returns()'在正確的地方打電話嗎? –