Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork84
Mocking Entity Framework Core operations such ToListAsync, FirstOrDefaultAsync etc. Author Raman Tsitou
License
ramantsitou/MockQueryable
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
Extensions for mockingEntity Framework Core async queries likeToListAsync,FirstOrDefaultAsync, and more using popular mocking libraries such asMoq,NSubstitute, andFakeItEasy — all without hitting the database.
❤️ If you really like the tool, please👉Support the project or☕Buy me a coffee.
Avoid hitting the real database in unit tests when querying viaIQueryable:
varquery=_userRepository.GetQueryable();awaitquery.AnyAsync(x=> ...);awaitquery.FirstOrDefaultAsync(x=> ...);awaitquery.ToListAsync();// etc.
varusers=newList<UserEntity>{newUserEntity{LastName="Smith",DateOfBirth=newDateTime(2012,1,20)},// More test data...};
varmock=users.BuildMock();// for IQueryable
_userRepository.Setup(x=>x.GetQueryable()).Returns(mock);
_userRepository.GetQueryable().Returns(mock);
A.CallTo(()=>userRepository.GetQueryable()).Returns(mock);
varmockDbSet=users.BuildMockDbSet();// Moqvarrepo=newTestDbSetRepository(mockDbSet.Object);// NSubstitute / FakeItEasyvarrepo=newTestDbSetRepository(mockDbSet);
mock.Setup(x=>x.FindAsync(userId)).ReturnsAsync((object[]ids)=>{varid=(Guid)ids[0];returnusers.FirstOrDefault(x=>x.Id==id);});
Build a mock with the customSampleLikeExpressionVisitor for testingEF.Functions.Like
varmockDbSet=users.BuildMockDbSet<UserEntity,SampleLikeExpressionVisitor>();
You can even create your own extensions. Check theexample here.
See thesample project for working examples.
About
Mocking Entity Framework Core operations such ToListAsync, FirstOrDefaultAsync etc. Author Raman Tsitou
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Sponsor this project
Uh oh!
There was an error while loading.Please reload this page.