3 回答
TA贡献1891条经验 获得超3个赞
我看到您DbContext在MovieRepository. 因此,不使用模拟,使用 EF CoreInMemory数据库将是您的绝佳选择。这也将降低复杂性。
编写您的GetAllTest()方法如下:
[Fact]
public void GetAllTest()
{
var options = new DbContextOptionsBuilder<MovieDbContext>()
.UseInMemoryDatabase(databaseName: "MovieListDatabase")
.Options;
// Insert seed data into the database using one instance of the context
using (var context = new MovieDbContext(options))
{
context.Movies.Add(new Movie {Id = 1, Title = "Movie 1", YearOfRelease = 2018, Genre = "Action"});
context.Movies.Add(new Movie {Id = 2, Title = "Movie 2", YearOfRelease = 2018, Genre = "Action"});
context.Movies.Add(new Movie {Id = 3, Title = "Movie 3", YearOfRelease = 2019, Genre = "Action"});
context.SaveChanges();
}
// Use a clean instance of the context to run the test
using (var context = new MovieDbContext(options))
{
MovieRepository movieRepository = new MovieRepository(context);
List<Movies> movies == movieRepository.GetAll();
Assert.Equal(3, movies.Count);
}
}
注意:不要忘记安装Microsoft.EntityFrameworkCore.InMemorynuget包如下:
安装包 Microsoft.EntityFrameworkCore.InMemory
TA贡献1788条经验 获得超4个赞
为了节省您的时间,请尝试使用我的 Moq/NSubstitute 扩展 MockQueryable:https ://github.com/romantitov/MockQueryable 支持所有同步/异步操作
//1 - create a List<T> with test items
var users = new List<UserEntity>()
{
new UserEntity,
...
};
//2 - build mock by extension
var mock = users.AsQueryable().BuildMock();
//3 - setup the mock as Queryable for Moq
_userRepository.Setup(x => x.GetQueryable()).Returns(mock.Object);
//3 - setup the mock as Queryable for NSubstitute
_userRepository.GetQueryable().Returns(mock);
也支持 DbSet
//2 - build mock by extension
var mock = users.AsQueryable().BuildMockDbSet();
//3 - setup DbSet for Moq
var userRepository = new TestDbSetRepository(mock.Object);
//3 - setup DbSet for NSubstitute
var userRepository = new TestDbSetRepository(mock);
笔记:
从 1.0.4 版本开始支持 AutoMapper
从 1.1.0 版本开始支持 DbQuery
从 3.0.0 版本开始支持 EF Core 3.0
TA贡献1797条经验 获得超6个赞
使用Moq.EntityFrameworkCore包。
这很简单:
var myDbContextMock = new Mock<MyDbContext>();
IList<Entity> entities = new List<Entity>() { new Entity(), new Entity() };
myDbContextMock.Setup(x => x.Entities).ReturnsDbSet(entities);
- 3 回答
- 0 关注
- 96 浏览
添加回答
举报