开发者

Is it possible to return a mock from another mock using Moq in C#?

开发者 https://www.devze.com 2023-01-10 23:52 出处:网络
I am using Moq as my mocking framework. As per the code below, I have two mocks setup and I would like to setup the second to return the first mock. Is this possible and if so how would I go about doi

I am using Moq as my mocking framework. As per the code below, I have two mocks setup and I would like to setup the second to return the first mock. Is this possible and if so how would I go about doing it? At the moment it says the mock being returned is an invalid candidate.

[SetUp]
private void SetupMarketRow()
{
   var marketTotalRow = new Mock<ITotalRow>();
   marketTotalRow.Setup(r => r.TotalBudgetCurrentFact).Returns(1860716);
   marketTotalRow.Set开发者_StackOverflow社区up(r => r.TotalBudgetEvol).Returns(-26);
   marketTotalRow.Setup(r => r.TotalBudgetPreviousFact).Returns(2514079);


   var localMarketReport = new Mock<IReport>();
   localMarketReport.Setup(r => r.TotalRow).Returns(marketTotalRow);  
   // Red swiggley here saying invalid candidate  

}


You can access the actual Mocked ITotalRow using marketTotalRow.Object.

[SetUp] 
private void SetupMarketRow() 
{ 
   var marketTotalRow = new Mock<ITotalRow>(); 
   marketTotalRow.Setup(r => r.TotalBudgetCurrentFact).Returns(1860716); 
   marketTotalRow.Setup(r => r.TotalBudgetEvol).Returns(-26); 
   marketTotalRow.Setup(r => r.TotalBudgetPreviousFact).Returns(2514079); 


   var localMarketReport = new Mock<IReport>(); 
   localMarketReport.Setup(r => r.TotalRow).Returns(marketTotalRow.Object);   
   // Red swiggley here saying invalid candidate   

}


Changing the interface declaration from

MarketTotalRow TotalRow { get; } 

to...

ITotalRow TotalRow { get; }

fixed the problem.

0

精彩评论

暂无评论...
验证码 换一张
取 消