当前位置:网站首页>Simulate EF dbcontext with MOQ - mocking EF dbcontext with MOQ
Simulate EF dbcontext with MOQ - mocking EF dbcontext with MOQ
2022-07-04 08:56:00 【Zafo】
problem :
I'm trying to create a unit test for my service with a mocked DbContext. I'm trying to use simulated DbContext Create unit tests for my service .I created an interface IDbContext with the following functions: I created an interface with the following functions IDbContext :
public interface IDbContext : IDisposable{ IDbSet<T> Set<T>() where T : class; DbEntityEntry<T> Entry<T>(T entity) where T : class; int SaveChanges();}My real context implements this interface IDbContext and DbContext . My real context implements this interface IDbContext and DbContext .
Now I'm trying to mock the IDbSet<T> in the context, so it returns a List<User> instead. Now I'm trying to simulate in context IDbSet<T> , So it goes back to List<User> .
[TestMethod]public void TestGetAllUsers(){ // Arrange var mock = new Mock<IDbContext>(); mock.Setup(x => x.Set<User>()) .Returns(new List<User> { new User { ID = 1 } }); UserService userService = new UserService(mock.Object); // Act var allUsers = userService.GetAllUsers(); // Assert Assert.AreEqual(1, allUsers.Count());}I always get this error on .Returns : I always get this mistake .Returns :
The best overloaded method match for'Moq.Language.IReturns<AuthAPI.Repositories.IDbContext,System.Data.Entity.IDbSet<AuthAPI.Models.Entities.User>>.Returns(System.Func<System.Data.Entity.IDbSet<AuthAPI.Models.Entities.User>>)'has some invalid argumentsSolution :
Reference resources : https://stackoom.com/en/question/1kvQm边栏推荐
- Awk from entry to earth (15) awk executes external commands
- 20220701 Barbalat引理证明
- Codeforces Global Round 21(A-E)
- go-zero微服务实战系列(九、极致优化秒杀性能)
- awk从入门到入土(8)数组
- [error record] no matching function for call to 'cacheflush' cacheflush();)
- 根据数字显示中文汉字
- GoLand environment variable configuration
- Horizon sunrise X3 PI (I) first boot details
- Report on research and investment prospects of polyglycolic acid industry in China (2022 Edition)
猜你喜欢

GoLand environment variable configuration

Guanghetong's high-performance 4g/5g wireless module solution comprehensively promotes an efficient and low-carbon smart grid

ArcGIS application (XXII) ArcMap loading lidar Las format data

【LeetCode 42】501. Mode in binary search tree

埃氏筛+欧拉筛+区间筛

How does Xiaobai buy a suitable notebook
![Langage C - démarrer - base - syntaxe - [opérateur, conversion de type] (vi)](/img/3f/4d8f4c77d9fde5dd3f53ef890ecfa8.png)
Langage C - démarrer - base - syntaxe - [opérateur, conversion de type] (vi)
![C language - Introduction - Foundation - syntax - [operators, type conversion] (6)](/img/3f/4d8f4c77d9fde5dd3f53ef890ecfa8.png)
C language - Introduction - Foundation - syntax - [operators, type conversion] (6)

09 softmax regression + loss function

Educational Codeforces Round 119 (Rated for Div. 2)
随机推荐
Flutter integrated amap_ flutter_ location
Manjaro install wechat
CLion-控制台输出中文乱码
How college students choose suitable computers
How to re enable local connection when the network of laptop is disabled
What is inner connection and outer connection? What are the uses and benefits
FOC control
1211 or chicken and rabbit in the same cage
Research and investment strategy report of China's electronic hydrogen peroxide industry (2022 Edition)
C language - Introduction - Foundation - syntax - [main function, header file] (II)
Awk from entry to earth (12) awk can also write scripts to replace the shell
std::is_ union,std::is_ class,std::integral_ constant
Awk from digging into the ground to getting started (10) awk built-in functions
awk从入门到入土(14)awk输出重定向
What exactly is DAAS data as a service? Don't be misled by other DAAS concepts
Educational Codeforces Round 115 (Rated for Div. 2)
How to play dapr without kubernetes?
Internal learning
awk从入土到入门(10)awk内置函数
How to send pictures to the server in the form of file stream through the upload control of antd