Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Mocking Entity Framework Core operations such ToListAsync, FirstOrDefaultAsync etc. Author Raman Tsitou

License

NotificationsYou must be signed in to change notification settings

ramantsitou/MockQueryable

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

184 Commits
 
 
 
 
 
 
 
 
 
 
 
 

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.


📦 NuGet Packages

PackageLatest VersionInstall via Package Manager
DownloadVersionInstall-Package MockQueryable.Core
DownloadVersionInstall-Package MockQueryable.EntityFrameworkCore
DownloadVersionInstall-Package MockQueryable.Moq
DownloadVersionInstall-Package MockQueryable.NSubstitute
DownloadVersionInstall-Package MockQueryable.FakeItEasy

✅ Build & Status

codecov.NET CoreLicense


⭐ GitHub Stats

StarsContributorsLast CommitCommit ActivityOpen Issues


💡 Why Use MockQueryable?

Avoid hitting the real database in unit tests when querying viaIQueryable:

varquery=_userRepository.GetQueryable();awaitquery.AnyAsync(x=> ...);awaitquery.FirstOrDefaultAsync(x=> ...);awaitquery.ToListAsync();// etc.

🚀 Getting Started

1. Create Test Data

varusers=newList<UserEntity>{newUserEntity{LastName="Smith",DateOfBirth=newDateTime(2012,1,20)},// More test data...};

2. Build the Mock

varmock=users.BuildMock();// for IQueryable

3. Set Up in Your favorite Mocking Framework

Moq

_userRepository.Setup(x=>x.GetQueryable()).Returns(mock);

NSubstitute

_userRepository.GetQueryable().Returns(mock);

FakeItEasy

A.CallTo(()=>userRepository.GetQueryable()).Returns(mock);

🗃️ MockingDbSet<T>

varmockDbSet=users.BuildMockDbSet();// Moqvarrepo=newTestDbSetRepository(mockDbSet.Object);// NSubstitute / FakeItEasyvarrepo=newTestDbSetRepository(mockDbSet);

🔧 Adding Custom Logic

Example: CustomFindAsync

mock.Setup(x=>x.FindAsync(userId)).ReturnsAsync((object[]ids)=>{varid=(Guid)ids[0];returnusers.FirstOrDefault(x=>x.Id==id);});

Example: Custom Expression Visitor

Build a mock with the customSampleLikeExpressionVisitor for testingEF.Functions.Like

varmockDbSet=users.BuildMockDbSet<UserEntity,SampleLikeExpressionVisitor>();

🧩 Extend for Other Frameworks

You can even create your own extensions. Check theexample here.


🔍 Sample Project

See thesample project for working examples.



[8]ページ先頭

©2009-2026 Movatter.jp