I am trying to use Moq.Automock
in one of my projects, which I have not used before. Please see the code below:
[TestFixture]
public class BusinessLayerTests
{
List<Denomination> expectedDenominations;
AutoMocker mocker = new AutoMocker();
UKCurrency Currency;
IDenominationFactory DenominationFactory;
[OneTimeSetUp]
public void Initialize()
{
Currency = mocker.CreateInstance<UKCurrency>();
DenominationFactory = mocker.CreateInstance<DenominationFactory>();
mocker.Setup<UKCurrency>(x => x.CurrencyDenominations()).Returns(CurrencyDenominations());
}
public System.Collections.Generic.IEnumerable<decimal> CurrencyDenominations()
{
yield return 50M;
}
}
I believe the code above shows that I have created a mock object called: Currency
. I believe the line starting: mocker.Setup
should ensure that the local method called: CurrencyDenominations
is called. However, this does not happen. The method called: CurrencyDenominations
in UKCurrency
is called.
What am I doing wrong?
You need to get the underlying mock and apply the setup on that.
[OneTimeSetUp]
public void Initialize() {
Currency = mocker.CreateInstance<UKCurrency>();
DenominationFactory = mocker.CreateInstance<DenominationFactory>();
var currencyMock = mocker.GetMock<UKCurrency>();
currencyMock.Setup(_ => _.CurrencyDenominations()).Returns(CurrencyDenominations());
}
provided UKCurrency.CurrencyDenominations
is virtual and able to be overridden.