aspnetboilerplate: Problem with IocManager and Test Project

I am using Test project generated by MVC Asp Net Core (Full Standard) template.

My LocalIocManager has more then 1000 registered components.

This property (LocalIocManager) is defined on AbpIntegratedTestBase<TStartupModule>.

I changed UserAppService and i need some another component that is registered on LocalIocManager.

When i run a unit test like:

[Fact]
public async Task GetUsers_Test()
{
    var _userAppService = Resolve<IUserAppService>();

    // Act
    var output = await _userAppService.GetAll(new PagedResultRequestDto
    {
        MaxResultCount = 20,
        SkipCount = 0
    });

    // Assert
    output.Data.Count().ShouldBeGreaterThan(0);
}

The method “Resolve” call resolve form LocalIocManager and _userAppService is created but IocManager property inside _userAppService is created empty, without registered components.

I need that the IocManager property create inside _userAppService be the same as LocalIocManager with all registered components.

Somebody Helps?

Thanks.

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Comments: 28 (13 by maintainers)

Most upvoted comments

acjh, thanks for your help!

I’ve created a template and checked that Property Injection is working !!. I found the problem in my project, it’s dependency on compiled assembly (Module) using “IocManager.Instance”. Now, with property injection, I can move on with my tests! I’ll refactor to use Constructor Injection as soon as I find the time

A clean fork of aspnetboilerplate/module-zero-core-template would be good.

It is used within Repository.GetAll() that implement IQueryable.

Do you mean with Repository.GetAll()?

var query = _scopeRepository.GetAll().ByScope();

I suggest using Domain Services:

var query = _scopeManager.GetScopes();

Someone know how make Property Injection work on Unit Test? using LocalIocManager to resolve objects?

Show your code.

Yes, this seems to be a good use case of property injection.

I have many entities that implement IUnidadeNegocioScope and i need to extend IQueryable<> with ByScope method that use AbpSession and UserManager to apply a filter on IQuerable.

public static IQueryable<IUnidadeNegocioScope<TEntity, int, TUnidadeNegocioFilialScope>> ByScope<TEntity, TUnidadeNegocioFilialScope>(this IQueryable<IUnidadeNegocioScope<TEntity, int, TUnidadeNegocioFilialScope>> GetAll)
    where TEntity : class, IEntity<int>, IUnidadeNegocioScope<TEntity, int, TUnidadeNegocioFilialScope> 
    where TUnidadeNegocioFilialScope : IUnidadeNegocioFilialScope<TEntity, int>
{
    using (var abpSession = IocManager.Instance.ResolveAsDisposable<IAbpSession>())
    using (var userManager = IocManager.Instance.ResolveAsDisposable<UserManager>())
    {
        var usuarioUnidadesNegocioIds = userManager.Object.Users
            .Include(usuario => usuario.UnidadeNegocios)
            .Where(usuario => usuario.Id == abpSession.Object.UserId)
            .SelectMany(usuario => usuario.UnidadeNegocios.Select(unidadeNegocio => unidadeNegocio.Id))
            .ToList();

        return GetAll.Where(e => e.UnidadeNegocioScopeId.HasValue && usuarioUnidadesNegocioIds.Contains(e.UnidadeNegocioScopeId.Value));
    }
}

I have other cases when i need IocManager.Instance but i can solve them if Property Injection work.