0
我在这里工作:)如何使用NSubstitute
我在创建将(使用实体框架)测试脚本被委托嘲笑EF查找值的表,如果存在。
我一起工作的代码具有此构造函数:
public PostProductHelper(
Func<IMachineDBContext> contextFactory)
{
_contextFactory = contextFactory;
}
我的单元测试方法可能是这样的:
public string CheckAndRemoveProductNameFileExtIfExists(
string productName)
{
using (var ctx = CreateContext())
{
return ctx.Products.FirstOrDefault(d => d.Name == productName);
}
}
因此,通过实例去谷歌上搜索,当我应该这样做:
MockProductRepository = Substitute.For<IProductRepository>();
MockMessagePublicationService = Substitute.For<IMessagePublicationService>();
MockMachineDBContext = Substitute.For<IMachineDBContext>(););
var Products = new List<Product>
{
new Product { Name = "BBB" },
new Product { Name = "ZZZ" },
new Product { Name = "AAA" },
}.AsQueryable();
MockMachineDBContext.Products.AddRange(Products);
但为了传递给我的构造函数,我必须修改这个到:
MockProductRepository = Substitute.For<IProductRepository>();
MockMessagePublicationService = Substitute.For<IMessagePublicationService>();
MockMachineDBContext = Substitute.For<Func<IMachineDBContext>>();
var Products = new List<Product>
{
new Product { Name = "BBB" },
new Product { Name = "ZZZ" },
new Product { Name = "AAA" },
}.AsQueryable();
MockMachineDBContext.Products.AddRange(Products);
最后一行说'无法解析符号'产品'的错误。
我不能改变这个构造函数,我很欣赏我可能会做一些正确的'咆哮',我现在仍然使用谷歌搜索,但也会感谢任何人的帮助。
感谢
这么简单!谢谢! –