title: Unit Testing Dapper with Inline Queries
body: <p>I know there are several question similar to mine.</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/20461553/dapper-unit-testing-sql-queries">Dapper: Unit Testing SQL Queries</a></li>
  <li><a href="https://stackoverflow.com/questions/8933052/testing-dapper-queries">Testing Dapper Queries</a> </li>
  </ul>
  
  <p>butI don't think both of above question has clear answer that fit my requirement.</p>
  
  <p>Right now I develop a new WebAPI project and split between WebAPI project and DataAccess technology. I not have a problem test the Controller for WebAPI since I can mock the data access class.</p>
  
  <p>But for DataAccess class that's a different stories, since I'm using Dapper with inline queries in it, I'm a bit confuse how can I test it by using Unit Test. I've asked some of my friends and they prefer to do Integration test instead of Unit Test.</p>
  
  <p>What I want to know is, is it possible to unit test the DataAccess class that use Dapper and Inline queries in it.</p>
  
  <p>Let's say I have a class like this (this is a generic repository class, since a lot of the codes have similar queries differentiate by table name and field)</p>
  
  <pre><code>public abstract class Repository&lt;T&gt; : SyncTwoWayXI, IRepository&lt;T&gt; where T : IDatabaseTable
  {
         public virtual IResult&lt;T&gt; GetItem(String accountName, long id)
         {
              if (id &lt;= 0) return null;
  
              SqlBuilder builder = new SqlBuilder();
              var query = builder.AddTemplate("SELECT /**select**/ /**from**/ /**where**/");
  
              builder.Select(string.Join(",", typeof(T).GetProperties().Where(p =&gt; p.CustomAttributes.All(a =&gt; a.AttributeType != typeof(SqlMapperExtensions.DapperIgnore))).Select(p =&gt; p.Name)));
              builder.From(typeof(T).Name);
              builder.Where("id = @id", new { id });
              builder.Where("accountID = @accountID", new { accountID = accountName });
              builder.Where("state != 'DELETED'");
  
              var result = new Result&lt;T&gt;();
              var queryResult = sqlConn.Query&lt;T&gt;(query.RawSql, query.Parameters);
  
              if (queryResult == null || !queryResult.Any())
              {
                  result.Message = "No Data Found";
                  return result;
              }
  
              result = new Result&lt;T&gt;(queryResult.ElementAt(0));
              return result;
         }
  
         // Code for Create, Update and Delete
    }
  </code></pre>
  
  <p>And the implementation for above code is like</p>
  
  <pre><code>public class ProductIndex: IDatabaseTable
  {
          [SqlMapperExtensions.DapperKey]
          public Int64 id { get; set; }
  
          public string accountID { get; set; }
          public string userID { get; set; }
          public string deviceID { get; set; }
          public string deviceName { get; set; }
          public Int64 transactionID { get; set; }
          public string state { get; set; }
          public DateTime lastUpdated { get; set; }
          public string code { get; set; }
          public string description { get; set; }
          public float rate { get; set; }
          public string taxable { get; set; }
          public float cost { get; set; }
          public string category { get; set; }
          public int? type { get; set; }
  }
  
  public class ProductsRepository : Repository&lt;ProductIndex&gt;
  {
     // ..override Create, Update, Delete method
  }
  </code></pre>