Base solution for your next web application
Open Closed

A UnitTest using IRepository always failed #1335


User avatar
0
heavenwing created

I hava a UnitTest, using DbContext directly is success, using IRepository always failed. My code is

[Fact]
public async Task Should_Get_EnterpriseAccounts()
{
    //Success
    var tenantId = AbpSession.TenantId.Value;
    await UsingDbContext(async context =>
    {
        var items1 = await context.WechatEnterpriseAccounts.Where(
            o => o.TenantId == tenantId).ToListAsync();
        items1.Count.ShouldBeGreaterThan(0);
    });

    //Failed
    var items2 = await _wechatAppService.GetEnterpriseAccounts();
    items2.Items.Count.ShouldBeGreaterThan(0);
}

public async Task<ListResultOutput<EnterpriseAccountListDto>> GetEnterpriseAccounts()
{
    var items = await _enterpriseAccountRepository.GetAll().ToListAsync();
    return new ListResultOutput<EnterpriseAccountListDto>(
        items.MapTo<List<EnterpriseAccountListDto>>());
}

public class EnterpriseAccount : FullAuditedEntity<Guid>, IMustHaveTenant
{
    public EnterpriseAccount()
    {

    }

    public EnterpriseAccount(string displayName)
    {
        DisplayName = displayName;
    }

    public virtual int TenantId { get; set; }

    [Required]
    [StringLength(64)]
    public virtual string DisplayName { get; set; }
}

But my GetEnterpriseAccounts method is work when test via swagger.


2 Answer(s)
  • User Avatar
    0
    heavenwing created

    When I disable MustHaveTenant Data Filter, it's work. So I believed that MustHaveTenant Data Filter don't get correct TenantId while Unit Test.

  • User Avatar
    0
    heavenwing created

    ok, this is a bug, see github [https://github.com/aspnetboilerplate/aspnetboilerplate/issues/1163])