Unit testing with queries defined in extension methods

后端 未结 8 739
执笔经年
执笔经年 2020-12-23 02:16

In my project I am using the following approach to querying data from the database:

  1. Use a generic repository that can return any type and is not bound to one t
8条回答
  •  悲&欢浪女
    2020-12-23 03:09

    The answer is (IMO): you should mock Query().

    The caveat is: I say this in total ignorance of how Query is defined here - I don't even known NHibernate, and whether it is defined as virtual.

    But it probably doesn't matter!Basically what I would do is:

    -Mock Query to return a mock IQueryable. (If you can't mock Query because it's not virtual, then create your own interface ISession, which exposes a mockable query, and so on.) -The mock IQueryable doesn't actually analyze the query it is passed, it just returns some predetermined results that you specify when you create the mock.

    All put together this basically lets you mock out your extension method whenever you want to.

    For more about the general idea of doing extension method queries and a simple mock IQueryable implementation, see here:

    http://blogs.msdn.com/b/tilovell/archive/2014/09/12/how-to-make-your-ef-queries-testable.aspx

提交回复
热议问题