当前位置:网站首页>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边栏推荐
- C language - Introduction - Foundation - syntax - [variable, constant light, scope] (V)
- Codeforces Global Round 21(A-E)
- Xcode 6 swift code completion does not work properly - Xcode 6 swift code completion not working
- C语言-入门-基础-语法-[标识符,关键字,分号,空格,注释,输入和输出](三)
- awk从入门到入土(9)循环语句
- Jianzhi offer 09 realizes queue with two stacks
- 根据数字显示中文汉字
- In depth research and investment strategy report on China's hydraulic parts industry (2022 Edition)
- How to play dapr without kubernetes?
- awk从入门到入土(15)awk执行外部命令
猜你喜欢
HMS core helps baby bus show high-quality children's digital content to global developers
Clion console output Chinese garbled code
How college students choose suitable computers
FOC控制
High order phase difference such as smear caused by myopic surgery
Newh3c - network address translation (NAT)
C language - Introduction - Foundation - syntax - [identifier, keyword, semicolon, space, comment, input and output] (III)
Jianzhi offer 09 realizes queue with two stacks
ctfshow web255 web 256 web257
CLion-控制台输出中文乱码
随机推荐
Dede plug-in (multi-function integration)
Mantis creates users without password options
Awk from entry to earth (14) awk output redirection
Use Alibaba cloud NPM image acceleration
Sequence model
Awk from getting started to digging in (11) detailed explanation of awk getline function
Openfeign service interface call
The basic syntax of mermaid in typera
The map set type is stored in the form of key value pairs, and the iterative traversal is faster than the list set
Codeforces Round #750 (Div. 2)(A,B,C,D,F1)
Implementation principle of redis string and sorted set
Bishi blog (13) -- oral arithmetic test app
awk从入门到入土(5)简单条件匹配
LinkedList in the list set is stored in order
Awk from entry to soil (5) simple condition matching
System disk expansion in virtual machine
Flutter 集成 amap_flutter_location
How to send pictures to the server in the form of file stream through the upload control of antd
2022 gas examination registration and free gas examination questions
Research Report on the current market situation and development prospects of calcium sulfate whiskers in China (2022 Edition)