当前位置:网站首页>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 arguments
Solution :
Reference resources : https://stackoom.com/en/question/1kvQm边栏推荐
- How college students choose suitable computers
- In depth research and investment strategy report on China's hydraulic parts industry (2022 Edition)
- Codeforces Global Round 21(A-E)
- awk从入门到入土(12)awk也可以写脚本,替代shell
- CLion-控制台输出中文乱码
- C language - Introduction - Foundation - syntax - [variable, constant light, scope] (V)
- Codeforces Round #750 (Div. 2)(A,B,C,D,F1)
- Newh3c - routing protocol (RIP, OSPF)
- How can we make a monthly income of more than 10000? We media people with low income come and have a look
- 《网络是怎么样连接的》读书笔记 - FTTH
猜你喜欢
L1 regularization and L2 regularization
How does Xiaobai buy a suitable notebook
Display Chinese characters according to numbers
User login function: simple but difficult
std::is_ union,std::is_ class,std::integral_ constant
HMS core helps baby bus show high-quality children's digital content to global developers
ArcGIS application (XXII) ArcMap loading lidar Las format data
C语言-入门-基础-语法-[变量,常亮,作用域](五)
C language - Introduction - Foundation - syntax - [operators, type conversion] (6)
Solve the problem of "Chinese garbled MySQL fields"
随机推荐
Basic discipline formula and unit conversion
根据数字显示中文汉字
Display Chinese characters according to numbers
ArcGIS application (XXII) ArcMap loading lidar Las format data
awk从入门到入土(5)简单条件匹配
2022 examination questions for safety managers of metal and nonmetal mines (underground mines) and examination papers for safety managers of metal and nonmetal mines (underground mines)
微服務入門:Gateway網關
go-zero微服务实战系列(九、极致优化秒杀性能)
Awk from getting started to digging in (11) detailed explanation of awk getline function
Mac platform forgets the root password of MySQL
Openfeign service interface call
Report on research and investment prospects of polyglycolic acid industry in China (2022 Edition)
Webapi interview question summary 01
There are 100 people eating 100 apples, one adult eating 4 apples, and four children eating 1 apple. How can they eat exactly 100 apples? Use any high-level language you are familiar with
How to pass custom object via intent in kotlin
The old-fashioned synchronized lock optimization will make it clear to you at once!
Service call feign of "micro service"
[BSP video tutorial] stm32h7 video tutorial phase 5: MDK topic, system introduction to MDK debugging, AC5, AC6 compilers, RTE development environment and the role of various configuration items (2022-
Jianzhi offer 09 realizes queue with two stacks
Xcode 6 swift code completion does not work properly - Xcode 6 swift code completion not working