Base solution for your next web application
Open Closed

Unit test issue hence Redis Cache #2485


User avatar
0
sampath created

Hi,

I have implemented Redis cache as shown below.But now on test method where it shows all the actual data on the database instead of the seed data.Because data was cached.Could you tell me how to avoid this situation? Thanks.

Note: I'm using Asp.net Zero

BankAppService.cs

public async Task<ListResultDto<BankListDto>> GetAllBanksAsync()
        {
            var cache = RedisConnectorHelper.Connection.GetDatabase();
            var values = RedisConnectorHelper.GetCache<BankListDto>(AppConsts.Banks, cache, o => o.Name);//get cache
            if (values != null) return values;

            var banks = await _bankRepository.GetAllListAsync();
            RedisConnectorHelper.SetCache<BankListDto, Bank>(AppConsts.Banks, banks, cache, o => o.Name);//set cache
            return new ListResultDto<BankListDto>(banks.OrderBy(o => o.Name).MapTo<List<BankListDto>>());
        }

Test method

BankAppServiceTests.cs

[Fact]
public async Task Test_GetAllBanksAsync()
 {
 //Act
var banks = await _bankAppService.GetAllBanksAsync();//here it shows over 7000+ records.which are on my db.But it must be just 4 records on the seed data no?

//Assert
banks.Items.ShouldContain(t => t.Name == "EquiCredit Corp of NY");
}

Seed method

namespace My.Company.Migrations.Seed
{
    public class InitialBanksCreator
    {
        private readonly IpDbContext _context;

        public InitialBanksCreator(IpDbContext context)
        {
            _context = context;
        }

        public void Create()
        {
            var object1 = _context.Banks.FirstOrDefault(p => p.Name == "EquiCredit Corp of NY");
            if (object1 == null)
            {
                _context.Banks.Add(
                    new Bank
                    {
                        Name = "EquiCredit Corp of NY",
                        TenantId = 2,
                    });
                _context.SaveChanges();
            }

            var object2 = _context.Banks.FirstOrDefault(p => p.Name == "Manufacturers & Traders Trust");
            if (object2 == null)
            {
                _context.Banks.Add(
                   new Bank
                   {
                       Name = "Manufacturers & Traders Trust",
                       TenantId = 2,
                   });
                _context.SaveChanges();
            }

            var object3 = _context.Banks.FirstOrDefault(p => p.Name == "Chemical Mtg Co");
            if (object3 == null)
            {
                _context.Banks.Add(
                   new Bank
                   {
                       Name = "Chemical Mtg Co",
                       TenantId = 2,
                   });
                _context.SaveChanges();
            }

            var object4 = _context.Banks.FirstOrDefault(p => p.Name == "Union Planters Bank");
            if (object4 == null)
            {
                _context.Banks.Add(
                   new Bank
                   {
                       Name = "Union Planters Bank",
                       TenantId = 2,
                   });
                _context.SaveChanges();
            }
        }
    }
}

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

    Hi,

    This is a common case independent from AspNet Zero. You can find documents on the web for unit testing redis depended code.

    But, if I would design the code, I wouldn't directly depend on redis, but I would use ABP's ICacheManager. Then you can use redis cache for production, memory cache for unit tests.

  • User Avatar
    0
    sampath created

    Hi,

    Yes, you're right.I'll redesign it using ICacheManager.Thanks a lot for the feedback :)

  • User Avatar
    0
    sampath created

    Hi,

    I would like to share another solution with the ABP community. If someone who has used the same kind of implementation what I have done then you can just clear the Redis cache on your development machine.After that no problem at all.Cheers ;)

  • User Avatar
    0
    ismcagdas created
    Support Team

    Thanks @sampath :)