document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. namespace UnitTestDemo
  2. {
  3.     using System.Collections.Generic;
  4.     using System.Data;
  5.     using System.Data.SqlClient;
  6.  
  7.     // We need a way to inject our mocked connection into the class under test.
  8.     // For that I created this interface and during test time I will override IConnectionFactory with Unity
  9.     public interface IConnectionFactory
  10.     {
  11.         IDbConnection GetConnection(string connectionString);
  12.     }
  13.  
  14.     public class SqlServerConnectionFactory : IConnectionFactory
  15.     {
  16.         public IDbConnection GetConnection(string connectionString)
  17.         {
  18.             return new SqlConnection(connectionString);
  19.         }
  20.     }
  21.  
  22.     public interface IDataProvider
  23.     {
  24.         List<Model> RetreiveSomeData(int envId);
  25.     }
  26.  
  27.     internal class DataProvider : IDataProvider
  28.     {
  29.         private readonly IConnectionFactory connectionFactory;
  30.  
  31.         // Injecting factory. In Unit test we can inject mocked factory that will return mock connection
  32.         public DataProvider(IConnectionFactory factory)
  33.         {
  34.             this.connectionFactory = factory;
  35.         }
  36.  
  37.         public List<Model> RetreiveSomeData(int envId)
  38.         {
  39.             const string ConnectionString = "Data Source=.;Initial Catalog=Test;Integrated Security=SSPI;";
  40.             using (var connection = connectionFactory.GetConnection(ConnectionString))
  41.             {
  42.                 connection.Open();
  43.  
  44.                 using (var command = connection.CreateCommand())
  45.                 {
  46.                     command.CommandText = "SELECT [EnvId],[ConStr] FROM [dbo].[ConnectionStrings] Where EnvId=@EnvId";
  47.                     command.CommandType = CommandType.Text;
  48.                    
  49.                     var param = command.CreateParameter();
  50.                     param.ParameterName = "@EnvId";
  51.                     param.Value = envId;
  52.                     param.DbType = DbType.Int32;
  53.  
  54.                     command.Parameters.Add(param);
  55.  
  56.                     using (var reader = command.ExecuteReader())
  57.                     {
  58.                         var result = new List<Model>();
  59.                         while (reader.Read())
  60.                         {
  61.                             var rEnvId = reader.GetInt32(reader.GetOrdinal("EnvId"));
  62.                             var rConStr = reader.GetString(reader.GetOrdinal("ConStr"));
  63.                             var model = new Model(rEnvId, rConStr);
  64.                             result.Add(model);
  65.                         }
  66.                         return result;
  67.                     }
  68.                 }
  69.             }
  70.         }
  71.     }
  72.  
  73.     public class Model
  74.     {
  75.         public readonly string Host;
  76.  
  77.         public readonly int LocationId;
  78.  
  79.         public Model(int locationId, string host)
  80.         {
  81.             LocationId = locationId;
  82.             Host = host;
  83.         }
  84.  
  85.         public override string ToString()
  86.         {
  87.             return string.Format("Host: {0}, LocationId: {1}", Host, LocationId);
  88.         }
  89.     }
  90. }
');