当前位置:网站首页>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从入门到入土(14)awk输出重定向
- C语言-入门-基础-语法-[变量,常亮,作用域](五)
- Codeforces Round #750 (Div. 2)(A,B,C,D,F1)
- Explain TCP protocol in detail three handshakes and four waves
- Sequence model
- awk从入门到入土(4)用户自定义变量
- Educational Codeforces Round 115 (Rated for Div. 2)
- 09 softmax regression + loss function
- [untitled] forwarding least square method
- awk从入门到入土(11)awk getline函数详解
猜你喜欢

HMS core helps baby bus show high-quality children's digital content to global developers
](/img/dc/5c8077c10cdc7ad6e6f92dedfbe797.png)
C语言-入门-基础-语法-[变量,常亮,作用域](五)

Educational Codeforces Round 115 (Rated for Div. 2)

Jianzhi offer 09 realizes queue with two stacks

09 softmax regression + loss function

Codeforces Round #793 (Div. 2)(A-D)

AI Winter Olympics | is the future coming? Enter the entrance of the meta universe - virtual digital human

Codeforces Round #750 (Div. 2)(A,B,C,D,F1)

OpenFeign 服务接口调用

Nurse level JDEC addition, deletion, modification and inspection exercise
随机推荐
What if the wireless network connection of the laptop is unavailable
Industry depression has the advantages of industry depression
Newh3c - routing protocol (RIP, OSPF)
C语言-入门-基础-语法-[主函数,头文件](二)
Explain TCP protocol in detail three handshakes and four waves
How to play dapr without kubernetes?
How to solve the problem of computer jam and slow down
C language - Introduction - Foundation - syntax - [identifier, keyword, semicolon, space, comment, input and output] (III)
The old-fashioned synchronized lock optimization will make it clear to you at once!
Service call feign of "micro service"
swatch
Getting started with microservices: gateway gateway
埃氏筛+欧拉筛+区间筛
到底什么才是DaaS数据即服务?别再被其他DaaS概念给误导了
Awk from getting started to digging in (11) detailed explanation of awk getline function
Turn: excellent managers focus not on mistakes, but on advantages
Basic discipline formula and unit conversion
Sequence model
C语言-入门-基础-语法-[运算符,类型转换](六)
如何编写单元测试用例