Is is possible to "mock" dbcontext?
I would like to write some unit tests for my services. For example, let's look at my method:
The problem is db context. I wanna test this method without real changes in my database so how can I do it?
The problem is db context. I wanna test this method without real changes in my database so how can I do it?
11 Replies
DbContextFactory is:
protected readonly ISegmentDbContextFactory DbContextFactory;
I think it's more common to create a DbContext that is in-memory, populate it with any sensible test data etc
I personally would test against a real database
just point your db context towards a test db
That too.
test db hmm
ok, thanks for advices
I'll try it
but in my opinion test db is a problem.. you always need to check data in db
wdym "check data in db"?
you should always check if there is data in the database. If the database is not filled with data, the tests may not work, and not exactly what I want
Not if you're creating a blank one and equipping it properly for each test - scaffoldable.
as the db is dedicated to testing, you your tests would just fill it with the needed data
smth like "fill database, test the method, remove what you filled before"?
it looks like in-memory
something like that
the reason I wouldn't use in-memory is it may not act like the real database