Click here to Skip to main content
15,916,432 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
using (var connect = new SqlConnection(_workFlowConnection))
              {

how to mock the above code in nunit unit testing

What I have tried:

Mock<sqlconnection> sqlMock=new Mock<sqlconnection>();
sqlMock.Setup(a => a.ConnectionString).Returns("");
Posted
Updated 14-Apr-16 1:31am
Comments
Tomas Takac 14-Apr-16 7:32am    
What are you trying to mock? The SqlConnection class?

1 solution

1) set up an interface for your datalayer
2) let your datalayer implement that interface (or extract the interface if you already have the datalayer ready)
3) now you can setup the mock for the interface

C#
public interface IDataLayerMyApp
{
   IEnumerable<string> GetMachineType(string werk);
}


public class DataLayerMyApp : IDataLayerMyApp
{
  public IEnumerable<string> GetMachineType(string werk)
  {
    IEnumerable<string> result;
    using (var connection = new SqlConnection(ConnectionString))
    {
      connection.Open();
      // do your work here
    }
    return result;
  }
}


[Test]
public void TheTestFunc()
{
   //Arrange
   var dal = new Mock<IDataLayerMyApp>();
   List<string> yourResult = ...
   dal.Setup(m => m.GetMachineType(It.IsAny<string>()))
      .Returns(yourResult)
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900