简体   繁体   中英

How to mock a return value?

I have a class:

 public class BankingTransfer : IBankingTransfer
 {
    public decimal GetTrustAccountBalance(string Id, string Pin)
    {
        throw new NotImplementedException();
    }

Now I want to do a xunit test on it. But not sure how to test a dummy return value?

My code:

 public class GetTrustAccountBalanceUnitTest
 {
    [Theory]
    [InlineData("1234")]
    [InlineData("1234")]
    public void GetTrustAccountBalance_Should_Return_A_Value(string expectedId,string expectedPin)
    {
        // ARRANGE
        var bankingTransferMock =new Mock<IBankingTransfer>();
        // ACT
        bankingTransferMock.Setup(x=>x.GetTrustAccountBalance(expectedId,expectedPin)).Returns()
        // ASSURE
    }
}

Just put a value inside the returns value like this:

bankingTransferMock.Setup(x=>x.GetTrustAccountBalance(expectedId,expectedPin)).Returns(1);

However, I think that you have misunderstood how to test the class that you have displayed. You shouldn't mock the class that you are trying to test. Going by the test name you probably want something more along these lines where you are just calling the method and seeing what is returned.

var bankingTransfer = new BankingTransfer();
Assert.True(bankingTransfer.GetTrustAccountBalance("", "") > 0);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM