我在测试上下文时遇到问题。
我的应用程序运行在.NET Core 2.2中,并且我已经安装了EFCore v2.2.6。
当我启动我的测试时,我得到了这个错误:
System.NotSupportedException:不支持的表达式:c=>c.价格在设置/验证表达式中不能使用不可重写的成员(此处: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,
CustomerAccount = "010324",
ItemId = "10103001",
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);
}
有人能帮我吗?
谢谢:(
如果在内存中使用,则无需模拟上下文。
[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,
CustomerAccount = "010324",
ItemId = "10103001",
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);
}