How can I stub IDBconnection

后端 未结 2 1181
抹茶落季
抹茶落季 2021-01-28 02:21

I am writting Unit Test for my database connection.

I have following class

Public class A
{
    public IDbConnection _dbConnection;

    public A()
    {         


        
2条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-28 02:37

    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);
    }
    

提交回复
热议问题