I have problems with mocking a method named Query that returns an interface IQueryable, and I don’t know why.
The method I try to mock with Moq framework:
public class ObjectContextRepository : IObjectContextRepository
{
.....
private ObjectContext _objectContext = null;
public IQueryable<TEntity> Query<TEntity>() where TEntity : class
{
// Moq Setup doesn't work and debugger enters this code:
return ObjectContext.CreateObjectSet<TEntity>();
}
...
}
the test example:
ObjectContextRepositoryFactory = new Mock<IObjectContextRepositoryFactory>();
ObjectContextRepositoryFactory.Setup(x => x.NewInstance(false))
.Returns(new ObjectContextRepository(It.IsAny<string>()));
CurrencyRateManager = new CurrencyRateManager(new ObjectContextRepositoryFactory("connection"));
ObjectContextRepository = new Mock<IObjectContextRepository>();
CurrencyExchangeRate rate1 = new CurrencyExchangeRate {EXCHANGE_DATE = new DateTime(2012, 09, 07)};
CurrencyExchangeRate rate2 = new CurrencyExchangeRate {EXCHANGE_DATE = new DateTime(2012, 09, 06)};
IList<CurrencyExchangeRate> list = new List<CurrencyExchangeRate> { rate1, rate2 };
// I wait that Query() method will return me a list with rates.
ObjectContextRepository.Setup(x => x.Query<CurrencyExchangeRate>()).Returns(list.AsQueryable());
using (IObjectContextRepository context = ObjectContextRepositoryFactory.Object.NewInstance())
{
// Mock doesn't work and debugger enters custom method context.Query<>() and throws an exception
var maxDateQuery = context.Query<CurrencyExchangeRate>()
.Where(c => c.EXCHANGE_DATE < new DateTime(2012, 09, 07));
}
PS. Yes, I know, that I have to use the integration tests, but it’s my task.
Your factory returns
new ObjectContextRepository(It.IsAny<string>())instead of your mock (which is even defined later that your factory). So your test executes against real implementation, not mock.BTW, there’s no sense in using
It.IsAny<string>()insideReturns, it does nothing.