ErikTJ
ErikTJ

Reputation: 2009

Moq Setup not working, the original method is still called

The original method it still called when i try to use Moq. Here is my code:

var mockedBetRepository = new Mock<BetRepository>(new FakeSiteContext());
mockedBetRepository.CallBase = true;
Bet bet = new Bet();
mockedBetRepository.Setup<Bet>(m => m.UpdateBet(bet)).Returns(bet);

betRepository = mockedBetRepository.Object;

Later in the code, betRepository.UpdateBet(bet) is called, but its not my mocked method which gets called, instead, the class's method gets called:

public virtual Bet UpdateBet(Bet betToUpdate)
{
    siteContext.Entry(betToUpdate).State = System.Data.EntityState.Modified;
    siteContext.SaveChanges();
    return betToUpdate;
}

Why it this happening?

Upvotes: 15

Views: 17998

Answers (2)

ErikTJ
ErikTJ

Reputation: 2009

I have found the problem.

If i replace

Bet bet = new Bet();
mockedBetRepository.Setup<Bet>(m => m.UpdateBet(bet)).Returns(bet);

with this

mockedBetRepository.Setup<Bet>(m => m.UpdateBet(It.IsAny<Bet>())).Returns((Bet b) => b);

Then it works.

Upvotes: 13

Andy
Andy

Reputation: 8562

Your setting callbase to true, which will call your actual implementation.

Upvotes: 0

Related Questions