chevron-thin-right chevron-thin-left brand cancel-circle search youtube-icon google-plus-icon linkedin-icon facebook-icon twitter-icon toolbox download check linkedin phone twitter-old google-plus facebook profile-male chat calendar profile-male
Welcome to Typemock Community! Here you can ask and receive answers from other community members. If you liked or disliked an answer or thread: react with an up- or downvote.
0 votes
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
asked by petteroe (3k points)

3 Answers

0 votes
Hi Morten,

Thanks for providing this information quickly, we thought that we got this worked out completely. We'll need to get to the bottom of this.
I'll check this first thing Sunday morning when I'm back in the office.
answered by gilz (14.5k points)
0 votes
Hi Gil,

I have tried to send you a couple of emails but your email server replies with an error. Do you have an IM we can use, or do you have an alternative email address where I can send you the files you requested?

Thanks,

Morten
answered by petteroe (3k points)
0 votes
Hi everyone,

We've got fixes for LINQ issues in version 4.3. Go download it at https://www.typemock.com/Downloads.php
answered by gilz (14.5k points)
...