测试期间出现 ObjectDisposedException

ObjectDisposedException during tests

阅读编辑

我有一个与 AsyncCrudAppService 类似的与过滤查询相关的实现。当我 运行 在派生于 AsyncCrudAppServiceBase 的应用程序服务的 ABP 实现之上进行测试时,一切 运行 都很好。当我在我的自定义 "filters" 之上执行相同的 运行ning 时,我收到以下错误:

System.ObjectDisposedException : Cannot access a disposed object [...]
Object name: 'DataManagerDbContext'.

我知道解决方案是使用 IUnitOfWorkManager 并调用 Begin() 方法来定义 UnitOfWork,但由于我正在使用 AppServices,我认为已经有一个 UnitOfWork 定义。这些是我的方法:

public PagedResultDto<StateDetails> GetEditorList(EditorRequestDto input)
{
    var query = _stateRepository.GetAllIncluding(p => p.Country).AsQueryable();

    query = ApplySupervisorFilter(query);

    query = query.ApplyFiltering(input, "Name");

    var totalCount = query.Count();

    query = query.ApplySorting<State, int, PagedAndSortedResultRequestDto>(input);

    query = query.ApplyPaging<State, int, PagedAndSortedResultRequestDto>(input);

    var entities = query.ToList();

    return new PagedResultDto<StateDetails>(totalCount, ObjectMapper.Map<List<StateDetails>>(entities));
}

private IQueryable<State> ApplySupervisorFilter(IQueryable<State> query)
{
    if (!SettingManager.GetSettingValue<bool>(AppSettingNames.SupervisorFlag))
    {
        query = ApplyUncategorizedFilter(query);
    }

    return query;
}

private IQueryable<State> ApplyUncategorizedFilter(IQueryable<State> query)
{
    return query.Where(
        p => !p.CountryId.HasValue);
}

我通过的测试(有手册UnitOfWork):

[Fact]
public async Task GetEditorListWithouSupervisorFlag_Test()
{
    using (UnitOfWorkManager.Begin())
    {
        await ChangeSupervisorFlag(false);

        var result = _stateAppService.GetEditorList(
            new EditorRequestDto
            {
                MaxResultCount = 10,

            });

        result.Items.Any(p => p.Country == null).ShouldBe(true);
    }
}

有人知道这个 "issue" 的解决方案吗?为我执行的每个测试定义一个 UnitOfWork 会很烦人。好像我做错了什么

编辑

我已经解决了这个问题。我必须在 运行ning 测试时为我的应用程序服务使用一个接口,以便它能够正确地模拟它

我已经解决了这个问题。我必须在 运行 测试时为我的应用程序服务使用一个接口,以便它能够正确地模拟它