单元测试:使用SQL查询创建和删除条目

时间:2018-08-03 08:08:45

标签: c# unit-testing

我对此很陌生,我正在尝试进行测试,以便可以在模拟数据库中创建和删除对象,但是我不知道现在从哪里开始或如何创建该Mock数据库并阅读。 /写入。

    [Fact]
    public void CreateCommentaar()
    {
        //arrange
        Commentaar commentaar = new Commentaar
        {
            CommentaarId = 0,
            StadId = 0,
            UserId = "testId",
            CommentaarText = "text",
            Tijdstip = DateTime.Now
        };

        string queryString = "INSERT INTO Commentaar(UserId, StadId, CommentaarText, Tijdstip) VALUES("
            + commentaar.CommentaarId + " ,"
            + commentaar.UserId + " ,"
            + commentaar.StadId + " ,"
            + commentaar.CommentaarText + " ,"
            + commentaar.Tijdstip + " ,";

        //act
        //Run Mock sql query

        //assert
        //1 commentaar object should exist in mock DB now

    }

    [Fact]
    public void DeleteCommentaar()
    {
        //arrange
        Commentaar commentaar = new Commentaar
        {
            CommentaarId = 0,
            StadId = 0,
            UserId = "testId",
            CommentaarText = "text",
            Tijdstip = DateTime.Now
        };

        string queryString = "INSERT INTO Commentaar(UserId, StadId, CommentaarText, Tijdstip) VALUES("
            + commentaar.CommentaarId + " ,"
            + commentaar.UserId + " ,"
            + commentaar.StadId + " ,"
            + commentaar.CommentaarText + " ,"
            + commentaar.Tijdstip + " ,";

        //act
        //Run Mock sql query

        //assert
        //1 commentaar object should be deleted
    }

我不太确定如何从这里继续。

2 个答案:

答案 0 :(得分:3)

这里真的没有要测试的东西。测试是否将某些内容插入到模拟数据库中将测试您的模拟逻辑。

最好使用集成测试和测试数据库来测试您的sql查询是否正确。

答案 1 :(得分:0)

您不能只是开始

string queryString = "SELECT COUNT(id) FROM Commentaar where id = 0"

如果该查询返回0,则表示您的Commentaar已被删除。

  

一个问题只有在您的第一个UT正常工作并插入ID为0的行时,它才有效。