C# Moq unable to return correct value

43 Views Asked by At

I am creating a Mock object to mock a method call that should return 1 using Moq in an MSTest unit test project to test an azure function.

My Interface -

public interface IMyInterface
{
    public int UpdateDB(MyClass obj);
}

My Class method implementation -

public int UpdateDB(MyClass obj)
{
    try
    {
        int i = 0;
        using (var db = dbContext)
        {
            db.myClassEntitity.Add(obj);
            i = db.SaveChanges();
        }
        return i;
    }
    //catch block
}

Calling the method in my azure function-

int rows = _dataUpdater.UpdateDB(obj);

Mocking code in my test project -

private readonly Mock<IMyInterface> _dbUpdater = new Mock<IMyInterface>();
_dbUpdater.Setup(p => p.UpdateDB(obj)).Returns(1);

My issue is whenever I am debugging the test and reaching the azure function, this code always returns 0,

int rows = _dataUpdater.UpdateDB(obj);

even though in my Mock setup I have asked to return 1. What could be issue, is there something I am missing?

0

There are 0 best solutions below