Different return values the first and second time with Moq

后端 未结 7 700
感动是毒
感动是毒 2021-01-29 18:51

I have a test like this:

    [TestCase("~/page/myaction")]
    public void Page_With_Custom_Action(string pat         


        
7条回答
  •  甜味超标
    2021-01-29 19:08

    Reached here for the same kind of problem with slightly different requirement.
    I need to get different return values from mock based in different input values and found solution which IMO more readable as it uses Moq's declarative syntax (linq to Mocks).

    public interface IDataAccess
    {
       DbValue GetFromDb(int accountId);  
    }
    
    var dataAccessMock = Mock.Of
    (da => da.GetFromDb(It.Is(acctId => acctId == 0)) == new Account { AccountStatus = AccountStatus.None }
    && da.GetFromDb(It.Is(acctId => acctId == 1)) == new DbValue { AccountStatus = AccountStatus.InActive }
    && da.GetFromDb(It.Is(acctId => acctId == 2)) == new DbValue { AccountStatus = AccountStatus.Deleted });
    
    var result1 = dataAccessMock.GetFromDb(0); // returns DbValue of "None" AccountStatus
    var result2 = dataAccessMock.GetFromDb(1); // returns DbValue of "InActive"   AccountStatus
    var result3 = dataAccessMock.GetFromDb(2); // returns DbValue of "Deleted" AccountStatus
    

提交回复
热议问题