This is a follow up from this thread:
https://www.typemock.com/community/viewtopic.php?t=727
I have now downloaded version 4.2.4 so that I would be able to mock LINQ to SQL statements. But I am still having problems. Can you please advice me how to correctly mock these statements?
Below is my code under test:
private Currencies()
{
loadCurrencies();
}
public static Currencies GetInstance()
{
if (_instance == null)
_instance = new Currencies();
return _instance;
}
private void getCurrencies()
{
Dictionary<string, Currency> tempList = new Dictionary<string, Currency>();
using (Data.CurrencyModelDataContext db = new Data.CurrencyModelDataContext())
{
var currencies = from currency in db.CurrencyEntities
select currency;
foreach (CurrencyEntity currency in currencies)
tempList.Add(currency.CurrencyCode, new Currency(currency.CurrencyCode, currency.NoOfDecimals, currency.ReceiveIsAllowed, currency.SendIsAllowed, currency.ExchangeRate, currency.ExchangeRateUpdated));
}
_list.Clear();
_list = tempList;
_lastUpdated = DateTime.UtcNow;
}
private void loadCurrencies()
{
if (!refreshIsRequired())
return;
getCurrencies();
}
and here is my test code:
[TestMethod]
public void GetInstance_NewInstance_RetrievesInstanceAndPopulatesCurrenciesFromDB()
{
Currency EUR = new Currency("EUR", 2, true, true, 1M, null);
Currency USD = new Currency("USD", 2, true, true, 1.28M, DateTime.Now);
var linqresult = new List<Currency>() { EUR, USD }.AsQueryable();
CurrencyModelDataContext context = new CurrencyModelDataContext();
using (RecordExpectations recorder = RecorderManager.StartRecording())
{
var mockedResult = from c in context.CurrencyEntities
select c;
recorder.Return(linqresult);
}
Currency Expected = Currency.EUR;
int expectedCount = 2;
Currency Actual = Currencies.GetInstance()["EUR"];
int actualCount = Currencies.GetInstance().Count;
Assert.AreEqual(Expected, Actual, "Initialization of Currencies didn't populate proper currencies");
Assert.AreEqual(expectedCount, actualCount, "Initialization of Currencies didn't populate with the two mocked currencies");
}
This produces the following test error:
Test method Towah.Global.CurrencyServices.UnitTests.CurrenciesTest.GetInstance_NewInstance_RetrievesInstanceAndPopulatesCurrenciesFromDB threw exception: TypeMock.TypeMockException:
*** Cannot return a value for Expression.Lambda() because no value was set. use recorder.Return().
*** Note: Cannot mock types from mscorlib assembly..
I have tried various versions of mocking context.CurrencyEntities directly (but I am unable to create a System.Data.Linq.Table<CurrencyEntity>, so the linq statement following fails). The code I have put above is how I would ideally like it to work.
Any help in getting me educated in mocking LINQ statements is greatly appreciated.
Morten