Base solution for your next web application
Open Closed

[Unit testing] DbSet is not returning any data from database #10584


User avatar
0
webmaster created

Prerequisites

Please answer the following questions before submitting an issue. YOU MAY DELETE THE PREREQUISITES SECTION.

  • What is your product version?

  • Not sure

  • What is your product type (Angular or MVC)?

  • angular

  • What is product framework type (.net framework or .net core)?

  • .net core

PartnerCenterDbContext.cs

   public virtual DbSet<Country> Countries { get; set; }

    public PartnerCenterDbContext(DbContextOptions<PartnerCenterDbContext> options)
        : 
        base(options)
    {
    }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);

        modelBuilder.ConfigurePersistedGrantEntity();
    }

Country_Tests.cs

   public class CountryAppService_Tests : AppTestBase
   {
    private readonly ICountryAppService _countryService;

    public CountryAppService_Tests()
    {
        _countryService = Resolve<ICountryAppService>();
        //LoginAsHostAdmin();
    }

    [Fact]
    public async Task US_ShouldBe491()
    {
        var usId = 491;

        var test = await _countryService.GetCountries();

        /* var us = countries.Where(c => c.Id == usId);
             us.ShouldNotBe(null);
             us.Count().ShouldBeGreaterThanOrEqualTo(1);*/

        UsingDbContext(
        context =>
        {
          var test2 = context.Countries.Select(b => b).ToList();
        });
    }
}

Country.cs

[Table("xxxCountries")]
public class Country : FullAuditedEntity
{
    [Required]
    [StringLength(CountryConsts.MaxNameLength, MinimumLength = CountryConsts.MinNameLength)]
    public virtual string Name { get; set; }

    [Required]
    [StringLength(CountryConsts.MaxCodeLength, MinimumLength = CountryConsts.MinCodeLength)]
    public virtual string Code { get; set; }

    [Required]
    [StringLength(CountryConsts.MaxCurrencyLength, MinimumLength = CountryConsts.MinCurrencyLength)]
    public virtual string Currency { get; set; }

    [Required]
    [StringLength(CountryConsts.MaxCurrencyCodeLength, MinimumLength = CountryConsts.MinCurrencyCodeLength)]
    public virtual string CurrencyCode { get; set; }

    public virtual double ExchangeRate { get; set; }

    public virtual int InternalId { get; set; }

}
internal class CountryConsts
{

    public const int MinNameLength = 0;
    public const int MaxNameLength = 50;

    public const int MinCodeLength = 0;
    public const int MaxCodeLength = 2;

    public const int MinCurrencyLength = 0;
    public const int MaxCurrencyLength = 50;

    public const int MinCurrencyCodeLength = 0;
    public const int MaxCurrencyCodeLength = 3;

}

I'm able to get data from dbContext in my test even though Im able to get them through Controller -> Service -> repository https://www.screencast.com/t/eGahTdZjUr is there some sort of a set up that I need to do?


4 Answer(s)
  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    For testing purposes, you can create data in https://github.com/aspnetboilerplate/aspnetboilerplate/blob/dev/test/Abp.ZeroCore.Tests/Zero/TestData/TestDataBuilder.cs or in the constructor of your test.

    Tests don't get data from your database.

  • User Avatar
    0
    webmaster created

    Hi, I'm asking why I get to valid return values for those Abp dbsets like UserAccounts but custom defined dbsets

    await UsingDbContextAsync(async context => { var test1 = context.Countries.ToList(); -> return 0 var test2 = context.ChatMessages.ToList(); -> return 0 var test3 = context.UserAccounts.ToList(); -> return 4 results

            });
    
  • User Avatar
    0
    musa.demir created

    Hi @webmaster AspNet Zero uses clean new InMemoryDb for every test case. In this way, all tests work independently from each other on a database basis, too. That's why you can not reach existing data from your database.

  • User Avatar
    0
    webmaster created

    Thank you for your help, we can close this case