How to unit test an SQL query?

前端 未结 4 1011
耶瑟儿~
耶瑟儿~ 2021-02-08 11:47

I have a class DBHandler which takes a query, runs it through the SQL server, checks for errors and returns the result. How can I unit test this class?

4条回答
  •  情歌与酒
    2021-02-08 12:33

    I'd use dependency injection to pass in the database connection or something similar, so that the whole thing can be mocked out in the tests. Then you can write tests where the mock query throws exceptions, returns various errors or valid results. Then your tests are just checking that DBHandler performs correctly.

提交回复
热议问题