Base solution for your next web application
Open Closed

IRepository<OrganizationUnit>.InsertAndGetIdAsync() isn't updating SqlLite DB during unit test #6216


User avatar
0
evadmin created

I'm unit testing some functioinality that involves creating organization units. I have no idea if what I've written works because the unit test continues to fail. I've narrowed it down to a situation in which new organization units aren't being added to the DbContext via IRepository<OrganizationUnit>.Insert() or IRepository<OrganizationUnit>.InsertAndGetIdAsync().

As you can see in the image above, according to the organizationUnitRepository which is injected into the constructor of the class this method is in, the collection of OrganizationUnits is unmodified. There are 14 units initialized directly in the DbContext during the test set up and code-first seed process. A new ID is returned, but the collection remains unmodified.

Tryng to assign the new ID as the parent to a new OrganizationUnit fails with the exception "There is no such entity. Entity type: Abp.Organizations.OrganizationUnit, id: 15". The same ID from the first image that was returned after IRespository<OrganizationUnit>.InsertAndGetIdAsync() was called.

I've spent about 13 hours trying to figure this out and I'm not making any progress. I'm desperate for help at this point.


3 Answer(s)
  • User Avatar
    1
    ismcagdas created
    Support Team

    Hi @evadmin

    OrganizationUnit is an IMayHaveTenant entity. So, when you get an OrganizationUnit using a repository, it will be automatically filtered by the current user's tenantId (In your case AbpSession.TennatId). So, if you don't set TenantId field of the inserted OU and tyr to get an OU if AbpSession.TenantId is different than null, you will get such an error.

    You can check the value of AbpSession.TenantId in your test method. Setting inserted OU's TenantId To AbpSession.TennatId might solve your problem.

  • User Avatar
    0
    evadmin created

    Bless you! That was the issue! I need a combination of a default TenantId and strategically placed calls to _unitOfWorkManager.Current.SaveChangesAsync(); to make it work, but the key was that I was creating org units with a null TenantId. Thank you so much! What a great way to kick 2019 off!

    Happy New Year! -Gabe

  • User Avatar
    0
    ismcagdas created
    Support Team

    Great :)