I am storing data into a database by calling a function, StoreData. In the designer code, a SqlConnection is open, SqlCommands are run, and the SqlConnection closes. After StoreData is run succesfully, data is stored in the DB.
I would like to run an integration test to StoreData by asserting that a value that I chose for the integration test (before StoreData runs, ie. id = 0, time = "13:00") is equal to the value found in the DB (after StoreData runs, ie. id column = 0 and time_stamp column = "13:00").
I thought about using LINQ. Is this a good approach, or is there a better approach to doing this?
Technically, this is an integration test, and it’s perfectly valid to make a test that will read the data back out of the database and ensure it looks correct.
However, if you want to make it a unit test, what you should do is mock the backing mechanism to be some kind of in memory structure, and then your
StoreDatamethod would insert into this structure instead of calling the database. Typically then, that would mean making some kind of factory mechanism forSqlConnectionandSqlCommandand then overloading methods to make sure the query gets targeted to your in memory structure.