Hacker News new | ask | show | jobs
by grey-area 2252 days ago
When you're developing, aren't you running a db server with a dev database for that? Just add another separate test db and use that for tests where appropriate.

Also, most unit tests don't need a db, it's more useful for integration tests.

2 comments

Hitting a database in tests is an order of magnitude slower and also involves worrying about a lot more code, i.e. the database engine. You may or may not want to take on that burden.
I use the db for some tests, it's totally fine, because it's limited to those tests which actually need db access (most don't). It's also closer to the actual application behaviour than mocks and doesn't need to be maintained.
If you're using a Repository pattern or similar you'll have a lot of unit tests where a database or db provider is an injected dependency.