升级到.net核心2后,单元测试失败

时间:2017-08-25 11:19:49

标签: .net-core entity-framework-core .net-core-2.0 .net-standard-2.0

有人可以向我解释这意味着什么,为什么我会得到它。

  

System.InvalidOperationException:从'VisitLambda'调用时,   重写“System.Linq.Expressions.ParameterExpression”类型的节点   必须返回相同类型的非null值。或者,覆盖   'VisitLambda'并将其更改为不访问此类儿童。

我从单元测试中得到它我正在使用EF核心运行最新的.net核心2。所有我的测试都很好,直到我升级然后我开始得到错误。

有趣的是,当我运行项目时,线路是否在测试中失败是可以的。

这是我的测试

[Fact]
        public async Task GetUserProfileAsync_Where_Employee_Exist_Test()
        {
            // Given
            var user = TestPrincipal.CreatePrincipalForEmployeeUser();

            using (var factory = new TestContextFactory())
            using (var context = factory.CreateInMemoryDatabase<ApplicationContext>())
            {
                this.SetDependencies(context);

                var data = EmployeeValueHelper.GetEmployeeValues();
                context.AddRange(data);
                context.SaveChanges();

                var sut = new ProfileService(new DbContextRepository<Data.Models.Employees.Employee>(context), this.userService, this.moqEmploymentStatusService.Object);

                // When
                // -> this method goes to a service and calls the below FindByIdAsync
                var actual = await sut.GetProfileForUserAsync(user);

                // Then
                Assert.Equal(10, actual.EmployeeId);
            }
        }


public async Task<Employee> FindByIdAsync(long id)
        {
            var profile = await this.repository.Set
                .Include(_ => _.Address) --> IT FAILS ON THIS LINE, IF I REMOVE THE INCLUDE THEN IT WORKS
                .Include(_ => _.EmployeeImage)
                .SingleOrDefaultAsync(_ => _.EmployeeId == id);

            if (profile == null)
            {
                return null;
            }

            return profile;
        }

更新

服务层

public class ProfileService : GenericService<Employee>, IProfileService
    {
        private readonly DbContextRepository<Employee> repository;
        private readonly IUserService userService;

        public ProfileService(DbContextRepository<Employee> repository, IUserService userService)
            : base(repository)
        {
            this.repository = repository;
            this.userService = userService;
        }

        public Task<Employee> GetProfileForUserAsync(ClaimsPrincipal user)
        {
            var id = this.userService.GetEmployeeId(user);
            return id.HasValue ? this.FindByIdAsync(id.Value) : null;
        }

        public async Task<Employee> FindByIdAsync(long id)
        {
            var profile = await this.repository.Set
                .Include(_ => _.Address)
                .Include(_ => _.EmployeeImage)
                .SingleOrDefaultAsync(_ => _.EmployeeId == id);

            if (profile == null)
            {
                return null;
            }

            return profile;
        }
    }

员工模式

public class Employee : IValidatableObject
    {
        [Key]
        [Column("pkEmpID")]
        public long EmployeeId { get; set; }

        [Column("fkCompanyID")]
        public long CompanyId { get; set; }

        public virtual Company Company { get; set; }

        [Display(Name = "lblEmpNumber")]
        public string EmpNumber { get; set; }

        public virtual IList<Address> Address { get; set; } = new List<Address>();

        // WITH SOME EXTRA STUFF NOT NEEDED FOR THIS

}

存储库

public class DbContextRepository<TEntity> : IGenericRepository<TEntity>, IDisposable
        where TEntity : class
    {
        public DbContextRepository(ApplicationContext context)
        {
            this.Context = context;
            this.Set = context.Set<TEntity>();
            this.SetWithNoTracking = this.Set.AsNoTracking();
        }

        public ApplicationContext Context { get; }

        public DbSet<TEntity> Set { get; }

        public IQueryable<TEntity> SetWithNoTracking { get; }

       // WITH SOME EXTRA STUFF NOT NEEDED FOR THIS

}

希望这会有更多的亮点

0 个答案:

没有答案