When unit testing, do you have to use a database to test CRUD operations?

后端 未结 7 1008
广开言路
广开言路 2021-02-09 21:11

When unit testing, is it a must to use a database when testing CRUD operations? Can sql lite help with this? Do you have to cre-create the db somehow in memory?

I am usi

7条回答
  •  甜味超标
    2021-02-09 21:30

    Really, if you are writing a test that connects to a database, you are doing integration testing, not unit testing.

    For unit testing such operations, consider using some typed of mock-database object. For instance, if you have a class that encapsulates your database interaction, extract an interface from it and then create an inheriting class that uses simple in-memory objects instead of actually connecting to the database.

提交回复
热议问题