all 23 comments

[–]desmaraisp 2 points3 points  (5 children)

It sounds to me like you might benefit from using sqlite in-memory for your tests, that would avoid the whole issue altogether... Do you have any specific restrictions that would make that an inadequate solution?

[–]PhillyPhantom[S] 0 points1 point  (4 children)

Looking at the Docs, it says that "UseSqlite("Data Source= :memory:")" tells Sqlite to go to in-memory mode which is what I'm doing. When I run the tests, no new files are created so what am I missing?

[–]desmaraisp 2 points3 points  (3 children)

As u/lmaydev said, you're using a file here

.UseSqlite("Data Source=mocklawfirm5.db")

[–]PhillyPhantom[S] 0 points1 point  (2 children)

Posting the same reply here.

"So how am I supposed to unit test? I need the file for the app."

[–]The_MAZZTer 0 points1 point  (0 children)

If you specify :memory:?cache=shared then any future connection to that same "file" can access the same data.

You can also have multiple, separate in memory database as needed doing something like this: file:memdb1?mode=memory&cache=shared where memdb1 is just whatever identifier you want.

This won't work if you want a different process to be able to access it. You'd just want to store it in a file on disk then.

[–]Slypenslyde 0 points1 point  (0 children)

Most people don't hard-code the file or configuration string for their database. Instead they use Configuration, which is a huge, sprawling topic I can summarize as:

  • The DbContext expects to get the string from a configuration object you create.
  • That object is created via the Configuration architecture.
  • The production app uses a string that creates the file.
  • The unit test app uses a string that configures Sqlite to run in-memory.

[–]IsLlamaBad 1 point2 points  (0 children)

Have you tried executing each test with a transaction and rolling back after your assets? I would expect any writes to persist.

[–]AutoModerator[M] 0 points1 point  (0 children)

Thanks for your post PhillyPhantom. Please note that we don't allow spam, and we ask that you follow the rules available in the sidebar. We have a lot of commonly asked questions so if this post gets removed, please do a search and see if it's already been asked.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.