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
Hi All,

Can someone please tell me why attempting to mock entityFramwork throws a compile time error on this line?

            Isolate.WhenCalled(() => contextHandle.LPRProvinceCodes.Find(0)).WillReturnCollectionValuesOf(pcodes.AsQueryable());

The error is: *** Method System.Data.Entity.DbSet`1[PlateTech.Shared.Models.LPRProvinceCode].Find returns PlateTech.Shared.Models.LPRProvinceCode. Can only use WillReturnCollectionValuesOf() on enumerable types.

Here is my test code:

        [TestMethod, Isolated]

        public void CheckpointTest2()

        {

            LPRInterfaceMessage lprMsg = new LPRInterfaceMessage()

            {

                   ... msg properties go here

            };

            Object[] lprsMessages = new Object[] { lprMsg };

            // Mock the EF context

            var contextHandle = Isolate.Fake.AllInstances<EFContext>(Members.CallOriginal);

            IList<LPRProvinceCode> pcodes = new List<LPRProvinceCode>()

            {

                new LPRProvinceCode()

                {

                    LPRCountryCodeId = 999,

                    ProvinceCode = \\\"999\\\",

                    ProvinceName = \\\"Unknown\\\"

                }

            };

            Isolate.WhenCalled(() => contextHandle.LPRProvinceCodes.Find(0)).WillReturnCollectionValuesOf(pcodes.AsQueryable());

            LPRInterface lpi = new LPRInterface();

            lpi.ThreadedAddLPREvent(lprsMessages);

        }

    }

here is the EF call that I am trying to mock:

public string FindProvinceByCode(string provinceCode)

        {

            if (int.TryParse(provinceCode, out int provinceCodeValue))

            {

                var results = _context.LPRProvinceCodes.Find(provinceCodeValue);

                if (results != null)

                {

                    return results.ProvinceName;

                }

            }

            return string.Empty;

        }
asked by Mark (4.1k points)
edited by Mark

2 Answers

0 votes

Hi Mark,

We are checking the issue and we will keep you posted.

Cheers,

Alon Sapozhnikov

Support Specialist

answered by Alon_TypeMock (8.9k points)
+1 vote

Hi Mark,

Can you please try replacing the line:

 "Isolate.WhenCalled(() => contextHandle.LPRProvinceCodes.Find(0)).WillReturnCollectionValuesOf(pcodes.AsQueryable());"

With the next line: 

"Isolate.WhenCalled(() => contextHandle.LPRProvinceCodes.Find(0)).WillReturn(pcodes[0]);"

Please keep us posted on the outcome.

Cheers,

Alon Sapozhnikov

Support Speciallist

answered by Alon_TypeMock (8.9k points)
This worked beautifully. As I am still new to typemock perhaps you could explain to me when to use the WillReturnCollection...??

Thanks,

Mark

Hi Mark,

You can read all about it in our documentation on the site.

Here is a link that refers to Faking the behavior of a live object:  http://www.typemock.com/docs/?book=Isolator&page=Documentation%2FHtmlDocs%2Ffakingbehaviorforliveobjects.htm

Cheers,

Alon Sapozhnikov

Support Specialist

...