测试最小起订量:System.NotSupportedException:不支持的表达式:c => c.Prices

我在测试上下文时遇到问题。

我的应用程序在.NET Core 2.2中运行,并且已经安装了EFCore v2.2.6。

启动测试时出现此错误:

System.NotSupportedException:不支持的表达式:c => c.Prices 不可覆盖的成员(此处为MyContext.get_Prices)不能在设置/验证表达式中使用。

这是我的上下文类:

using MyProject.Model;
using microsoft.EntityFrameworkCore;

namespace MyProject.Persistence
{
    public class MyContext : DbContext
    {
        public MyContext(DbContextOptions<MyContext> options) : base(options) {}
        protected override void OnmodelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Price>()
                .HasKey(p => new { p.Customeraccount,p.ItemId,p.Amount });

        }

        public DbSet<Price> Prices { get; set; }
    }
}

这是我的存储库:

using MyProject.Model;
using MyProject.Persistence;
using microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace MyProject.Repository
{
    public class PriceRepository : IPriceRepository
    {
        private readonly MyContext _myContext;


        public PriceRepository(MyContext myContext)
        {
            _myContext = myContext;
        }

        public async Task<List<Price>> GetPricesAsync(List<string> items,string customeraccount)
                => await _myContext.Prices.Where(price => price.Customeraccount == customeraccount && items.Contains(price.ItemId)).ToListAsync();

    }
}

我的价格类别:

    [Table("Price")]
    public class Price
    {

        public string Customeraccount { get; set; }
        public string ItemId { get; set; }
        public double Amount { get; set; }
        [NotMapped]
        public int Id { get; set; }
        [NotMapped]
        public string ItemInternalId { get; set; }
        [NotMapped]
        public DateTime ModifiedDateTime { get; set; }
    }

我的测试:

    [Fact]
    public async Task Test1Async()
    {


        IQueryable<Price> prices = new List<Price>
        {
                new Price
                {
                    Amount = 39.71,Customeraccount = "010324",ItemId = "10103001",Id = 1,ItemInternalId = "test",ModifiedDateTime = new System.DateTime()
                },new Price
                {
                    Amount = 57.09,Id = 2,ItemInternalId = "test2",ModifiedDateTime = new System.DateTime()
                }

        }.AsQueryable();

        var mockSet = new Mock<DbSet<Price>>();

        var options = new DbContextOptionsBuilder<MyContext>()
                    .UseInmemoryDatabase(databaseName: "FekaConnectionString")
                    .Options;

        mockSet.As<IQueryable<Price>>().Setup(m => m.Provider).Returns(prices.Provider);
        mockSet.As<IQueryable<Price>>().Setup(m => m.Expression).Returns(prices.Expression);
        mockSet.As<IQueryable<Price>>().Setup(m => m.ElementType).Returns(prices.ElementType);
        mockSet.As<IQueryable<Price>>().Setup(m => m.GetEnumerator()).Returns(prices.GetEnumerator());

        var mockContext = new Mock<MyContext>(options);

        mockContext.Setup(c => c.Prices).Returns(mockSet.Object);

        var repository = new PriceRepository(mockContext.Object);

        var list = new List<string>
        {
            "10103001"
        };

        var result = await repository.GetPricesAsync(list,"010324");

        Assert.Single(result);
    }

有人可以帮助我吗?

谢谢:)

dezhaochao 回答:测试最小起订量:System.NotSupportedException:不支持的表达式:c => c.Prices

如果使用内存,则无需模拟上下文。

[Fact]
public async Task Test1Async() {
    //Arrange
    var prices = new List<Price> {
        new Price {
            Amount = 39.71,CustomerAccount = "010324",ItemId = "10103001",Id = 1,ItemInternalId = "test",ModifiedDateTime = new System.DateTime()
        },new Price
        {
            Amount = 57.09,Id = 2,ItemInternalId = "test2",ModifiedDateTime = new System.DateTime()
        }
    };

    var options = new DbContextOptionsBuilder<MyContext>()
                .UseInMemoryDatabase(databaseName: "FekaConnectionString")
                .Options;

    var context = new MyContext(options);
    //populate
    foreach(var price in prices) {
        context.Prices.Add(price);
    }
    await context.SaveChangesAsync();

    var repository = new PriceRepository(mockContext.Object);

    var list = new List<string>
    {
        "10103001"
    };

    //Act
    var result = await repository.GetPricesAsync(list,"010324");

    //Assert
    Assert.Single(result);
}
本文链接:https://www.f2er.com/3064874.html

大家都在问