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. I've got a generic method that tests the existence of an entity in a table:
public class Repository
   private readonly MyDataContext db;

   private bool EntityExists<TEntity>(TEntity entity) where TEntity : class
        {
            //return db.GetTable<TEntity>().Any(e => e == entity);
            IQueryable<TEntity> table = db.GetTable<TEntity>();
            bool exists = table.Any<TEntity>(e => e == entity);

            return exists;
        }
}


In my test I mock the data context as follows:
MockManager.Init();
Mock mockDataContext = MockManager.Mock(typeof(TokServicesDataContext));
List<Category> mockCategories = new List<Category>();
mockDataContext.AlwaysReturn("GetTable", mockCategories);
MyRepository rep = new ServicesRepository(new ServicesDataContext(""));


And test like this:
Category cat = new Category{ ID = 1, Name = "A cat", Code = "ABCD1234" };
mockCategories.Add(cat);
bool exists = rep.EntityExists<Category>(cat);
Assert.IsTrue(exists);


When I run the test, it mocks successfully, passing the mock-categories list to DataContext.GetTable<Category>().
However when I run the Any<TEntity>() method it crashes, with a
System.EntryPointNotFoundException. The message was a non descriptive "Entry point was not found".

I can't figure out what is wrong here. Could anybody explain it to me?
Thanks,
Assaf.
asked by abstone (1.3k points)

5 Answers

0 votes
Hi Assaf,

The problem that you see is caused by a mismatching return type.

db.GetTable<TEntity>() returns an object of type IQueryable<TEntity>, and in the mocking part you told it to return an object of type List<Category>.

About the error message, you are absolutely right and the error is completely non descriptive. Furthermore the exception should be thrown on the call to AlwaysReturn and not during the test run. :cry:

Thank you for pointing this out. I will let you know after this will be fixed.
answered by lior (13.2k points)
0 votes
Thanks for pointing it out.

I feel like an idiot. My [Setup] method is packed full of expectations, returning List.AsQueryable(). And just this one that I added was returned as a List.

I feel sheepish.
answered by abstone (1.3k points)
0 votes
Thanks for pointing it out.

I feel like an idiot. My [Setup] method is packed full of expectations, returning List.AsQueryable(). And just this one that I added was returned as a List.

:oops: :oops: :oops:
answered by abstone (1.3k points)
0 votes
Glad I could help.

But as you pointed out the exception thrown should have pointed you to the problem.
answered by lior (13.2k points)
0 votes
Hi,

We have included a fix for this in the new 4.22 release.
supplied return values are now validated against the real return type, and a proper exception is thrown in case of a mismatch.
answered by lior (13.2k points)
...