首页 > 解决方案 > 此函数只能在单元测试期间从 LINQ to Entities 错误中调用

问题描述

嗨,我在测试我的方法时遇到问题。我将我的存储库模拟为 DbSet。我的单元测试方法看起来是:

public void Can_change_quantity_of_product()
    {
        //przygotowanie
        var repo = CreateDbSetMock(new List<Processor>
        {
            new Processor{Product_ID = 1, Quantity = 20},
            new Processor{Product_ID = 2, Quantity = 5}
        });
        var target = new ProductContext();
        target.context.Processors = repo.Object;
        //działanie
        target.SellProduct(new OrderDetails { Product_ID = 1, Quantity = 5 });
        //Asercje
        Assert.AreEqual(target.TakeProcessors.ToList()[0].Quantity, 15);
    }

我在这里得到这个错误:

public IEnumerable<Product> TakeProcessors  //Zwraca repozutorium procesorów jako lista klas Product.
    {
        get
        {
            return (from x in context.Processors
                    select new Product
                    {
                        ProductID = x.Product_ID,
                        Title = x.Title,
                        Price = x.Price,
                        Quantity = x.Quantity,
                        FDetail = SqlFunctions.StringConvert(x.Clock, 5, 1),
                        SDetail = SqlFunctions.StringConvert((double)x.Cores),
                        TDetail = SqlFunctions.StringConvert((decimal)x.Cache),
                        OrderDetails = x.OrderDetails,
                        Promoted = x.Promoted,
                        SoldPieces = x.OrderDetails.Count() == 0 ? 0 : x.OrderDetails.Sum(y => y.Quantity),
                        Category = "Procesory"
                    }).ToList();
        }
    }

我怎么了?

标签: c#entity-frameworklinqunit-testing

解决方案


推荐阅读