2016-06-25 4 views
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); 
    } 

문제이며, Execute 메서드는 0을 returing하고 있지만 내 MOQ에서는 4을 설정하고 있습니다. 왜 이런 일이 일어나는가? 에서

+0

'Returns()'호출이 올바른 위치에 있습니까? –

답변

2

당신의 Setup 당신은 그 인수가 대신 사용한다 2,2

있습니다 Execute에 인수를 일치하지 않는, IsAny(1,2) 말 :

instance.Setup(moq => moq.Sum(It.IsAny<int[]>()).Returns(4)); 

(추가 정보를 원하시면 Setup Method With Params Array 참조)

+0

감사합니다. –

관련 문제