最小起订量单元测试 - 值不能为空
Unit Test with Moq - Value can not be null
我正在使用 EF6。生成的代码类似于:
public partial class MyDataContext : DbContext
{
public MyDataContext() : base("name=mydata")
{
}
public virtual DbSet<Book> Books { get; set; }
}
然后我有一个通用存储库,如:
public class GenericRepository<TObject> where TObject : class
{
protected readonly MyDataContext Context;
protected GenericRepository(MyDataContext context)
{
Context = context;
}
public virtual DbSet<TObject> GetAll()
{
return Context.Set<TObject>();
}
}
然后我有一个使用 GenericRepository 的服务 return 数据:
public class MyDataService<TObject> where TObject : class
{
private readonly MyDataContext context;
public MyDataService(MyDataContext ct)
{
context = ct;
}
public ICollection<TObject> GetAll()
{
var r = new GenericRepository<TObject>(context);
return r.GetAll().ToList();
}
}
所以我可以得到所有类似这样的书:
var ds = new MyDataService<Book>(new MyDataContext());
var data = ds.GetAll();
这工作正常。接下来我尝试使用 Moq 对上面的代码进行单元测试,例如:
var books = new List<Book>
{
new Book {Id = 1, Name = "BBB"},
new Book {Id = 2, Name = "ZZZ"},
new Book {Id = 3, Name = "AAA"},
}.AsQueryable();
var mockSet = new Mock<DbSet<Book>>();
mockSet.As<IQueryable<Book>>().Setup(m => m.Provider).Returns(books.Provider);
mockSet.As<IQueryable<Book>>().Setup(m => m.Expression).Returns(books.Expression);
mockSet.As<IQueryable<Book>>().Setup(m => m.ElementType).Returns(books.ElementType);
mockSet.As<IQueryable<Book>>().Setup(m => GetEnumerator()).Returns(books.GetEnumerator());
var mockContext = new Mock<MyDataContext>();
mockContext.Setup(c => c.Books).Returns(mockSet.Object);
var service = new MyDataService<Book>(mockContext.Object);
var data = service.GetAll();
但是,我在最后一行收到 "Value cannot be null.\r\nParameter name: source"
错误。当我进入代码时,我看到上下文对象中的 Books 集合是空的。
我做错了什么?
那是因为测试在数据上下文中设置了 .Setup(c => c.Books)
但在实际代码中访问 Context.Set<TObject>()
在 GetAll()
方法中,所以对于测试它最终会是 null
.
尝试更改为
mockContext.Setup(c => c.Set<Book>()).Returns(mockSet.Object);
我正在使用 EF6。生成的代码类似于:
public partial class MyDataContext : DbContext
{
public MyDataContext() : base("name=mydata")
{
}
public virtual DbSet<Book> Books { get; set; }
}
然后我有一个通用存储库,如:
public class GenericRepository<TObject> where TObject : class
{
protected readonly MyDataContext Context;
protected GenericRepository(MyDataContext context)
{
Context = context;
}
public virtual DbSet<TObject> GetAll()
{
return Context.Set<TObject>();
}
}
然后我有一个使用 GenericRepository 的服务 return 数据:
public class MyDataService<TObject> where TObject : class
{
private readonly MyDataContext context;
public MyDataService(MyDataContext ct)
{
context = ct;
}
public ICollection<TObject> GetAll()
{
var r = new GenericRepository<TObject>(context);
return r.GetAll().ToList();
}
}
所以我可以得到所有类似这样的书:
var ds = new MyDataService<Book>(new MyDataContext());
var data = ds.GetAll();
这工作正常。接下来我尝试使用 Moq 对上面的代码进行单元测试,例如:
var books = new List<Book>
{
new Book {Id = 1, Name = "BBB"},
new Book {Id = 2, Name = "ZZZ"},
new Book {Id = 3, Name = "AAA"},
}.AsQueryable();
var mockSet = new Mock<DbSet<Book>>();
mockSet.As<IQueryable<Book>>().Setup(m => m.Provider).Returns(books.Provider);
mockSet.As<IQueryable<Book>>().Setup(m => m.Expression).Returns(books.Expression);
mockSet.As<IQueryable<Book>>().Setup(m => m.ElementType).Returns(books.ElementType);
mockSet.As<IQueryable<Book>>().Setup(m => GetEnumerator()).Returns(books.GetEnumerator());
var mockContext = new Mock<MyDataContext>();
mockContext.Setup(c => c.Books).Returns(mockSet.Object);
var service = new MyDataService<Book>(mockContext.Object);
var data = service.GetAll();
但是,我在最后一行收到 "Value cannot be null.\r\nParameter name: source"
错误。当我进入代码时,我看到上下文对象中的 Books 集合是空的。
我做错了什么?
那是因为测试在数据上下文中设置了 .Setup(c => c.Books)
但在实际代码中访问 Context.Set<TObject>()
在 GetAll()
方法中,所以对于测试它最终会是 null
.
尝试更改为
mockContext.Setup(c => c.Set<Book>()).Returns(mockSet.Object);