0

I'm new to unit testing and i need to test a insert command in my code. Can anyone please let me know how to do it.. Following is the method i want to test.

    public void InsertData()
    {        
        Connect();    
        query = "Insert into Person values ('"+ name +"','"+ address +"','"+ 
        phn +"')";            
        cmd = new SqlCommand(query, Conn);                      
        cmd.ExecuteNonQuery();
        DisConnect();
    }
2
  • you nead read: msdn.microsoft.com/en-us/library/ms182532.aspx Commented May 4, 2016 at 8:29
  • You have to change your method to test in a way that it uses instances without references to a database. As far as I see you need context Interfaces which have methods like connect and disconnect and command interfaces which have methods like ExecueNonQuery and ExecuteQuery and so on... When the method is refactored you can test the method. Maybe you should use dependency injection too. Commented May 4, 2016 at 8:52

1 Answer 1

3

At the moment your method is not testable. All references to real data should be avoided.

This would be a refactored method:

public interface IDbContext
{
    void Connect();
    void DisConnect();
    IDbCommand GetDbCommand(string query, string[] parameters);
}

public class DbContext : IDbContext
{
    public IDbConnection Conn { get; set; }

    public void Connect()
    {
        // your code here
    }

    public void DisConnect()
    {
        // your code here
    }

    public IDbCommand GetDbCommand(string query, string[] parameters)
    {
        // parameter handling
        return new SqlCommand(query, (SqlConnection)Conn);
    }
}

public class YourClass
{
    private string name;
    private string address;
    private string phn;

    public void InsertData(IDbContext context)
    {
        context.Connect();
        var cmd = context.GetDbCommand("Insert into Person values ('{0}','{1}','{2}')", new string[] { name, address, phn });
        cmd.ExecuteNonQuery();
        context.DisConnect();
    }
}

To test this you can do it like this method:

[TestClass]
public class TestClass
{
    [TestMethod]
    public void TestMethod1()
    {
        var instance = new YourClass();

        // create an instance of IDbContext
        var context = new Mock<IDbContext>();
        // create an instance of IDbCommand
        var command = new Mock<IDbCommand>();

        // setup your context and what should be the return of any method you want
        context.Setup(c => c.GetDbCommand(It.IsAny<string>(), It.IsAny<string[]>())).Returns(command.Object);

        // call your method you want to test
        instance.InsertData(context.Object);

        // assert that context methods ar called
        context.Verify(c => c.Connect(), Times.Once);
        context.Verify(c => c.DisConnect(), Times.Once);
        context.Verify(c => c.GetDbCommand(It.IsAny<string>(), It.IsAny<string[]>()), Times.Once);

        // assert that command methods ar called
        command.Verify(c => c.ExecuteNonQuery(), Times.Once);
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.