0

I am writting Unit Test for my database connection.

I have following class

Public class A
{
    public IDbConnection _dbConnection;

    public A()
    {
       _dbConnection = new SqlConnection(connectionStringName);
    }

    public int ExecuteNoneQuery(CommandDefination command)
    {
       return _dbConnection.Execute(command);
    }
}

I want to test this class, how I can test with Microsoft Stub/Shim

I have written following code, but it is not working.

[TestMethod]
public void TestMethod1()
{
    StubIDbConnection stubIDbConnection = new StubIDbConnection();
    stubIDbConnection.Execute =(null) => -1;
    var a = new classA();
    int answer = a.ExecuteNoneQuery(null);
    Assert.AreEqual(-1, answer);

 }
Ardalan Shahgholi
  • 11,967
  • 21
  • 108
  • 144
Hardik Shah
  • 3
  • 1
  • 6

2 Answers2

0

The method execute doesn't exist in IDbConnection / SqlConnection. Therefore I assume that you have created a custom interface and class.

The right way to test your code is to change the code into "code that designed to be a testable":

public class A
{
    public IDbConnection _dbConnection;

    public A() : this(new SqlConnection()){}

    public A(IDbConnection connection)
    {
        _dbConnection = connection;
    }
}

Now you can inject your fake connection:

[TestMethod]
public void TestMethod1()
{
    StubIDbConnection stubIDbConnection = new StubIDbConnection();
    stubIDbConnection.Execute =(null) => -1;
    var a = new classA(stubIDbConnection);
    int answer = a.ExecuteNoneQuery(null);
    Assert.AreEqual(-1,-1);
}

If you don't want to change your code. You have to create shim, and then change your test method:

    [TestMethod]
    public void TestMethod1()
    {
        using (ShimsContext.Create())
        {
            System.Data.SqlClient.Fakes.ShimSqlConnection.AllInstances.Execute = 
            (connection, command) =>
            {
                if (command != null)
                    throw new Exception("command is not null");

                return -1;
            };

            var a = new classA();
            int answer = a.ExecuteNoneQuery(null);
            Assert.AreEqual(-1, -1);
        }
    }

Edit

The problem you've faced occurred because Execute method has several overloads.

Change your test method to:

    [TestMethod]
    public void TestMethod1()
    {
        using (ShimsContext.Create())
        {
            Dapper.Fakes.ShimSqlMapper.ExecuteIDbConnectionCommandDefinition =
                (connection, command) =>
                {
                    //add here params verification...

                    return -1;
                };

            var a = new A();
            int answer = a.ExecuteNoneQuery(new CommandDefinition());
            Assert.AreEqual(-1, answer);
        }
    }
Community
  • 1
  • 1
Old Fox
  • 8,629
  • 4
  • 34
  • 52
  • I am using Dapper (Third Party Library) to execute my sql, so I can not modify code, I have to use Shim. Below are the my code, but I am getting compiler error [Error 102 Cannot assign to 'Execute' because it is a 'method group' ] at line no 5 – Hardik Shah May 04 '15 at 15:47
  • 1 using (ShimsContext.Create()) 2 { 3 4 var shimSqlConnection = new System.Data.SqlClient.Fakes.ShimSqlConnection(); 5 shimSqlConnection.Instance.Execute = (connection, command) => 6 { 7 if (command != null) 8 throw new Exception("command is not null"); 9 10 return -1; 11 }; 12 } – Hardik Shah May 04 '15 at 15:52
  • @HardikShah The Information on using Dapper was necessary to find a solution. I edited my answer... – Old Fox May 04 '15 at 18:56
0

You can easely use the constructor to of class A to insert your dependencies, add the following constructor to class A:

public A(IDBConnection connection)
{
   _dbConnection = connection;
}

Then you test will look like:

[TestMethod]
public void TestMethod1()
{
    StubIDbConnection stubIDbConnection = new StubIDbConnection();
    stubIDbConnection.Execute =(null) => -1;
    var a = new classA(stubIDbConnection);
    int answer = a.ExecuteNoneQuery(null);
    Assert.AreEqual(-1,answer);
}
Peter
  • 27,590
  • 8
  • 64
  • 84