abp icon indicating copy to clipboard operation
abp copied to clipboard

Unit Testing with more than one DbContext

Open Wello86 opened this issue 6 years ago • 9 comments

Hi,

Is unit testing with 2 db contexts working? I am struggling in it and till now the test methods can not find the inserted data. I have used the help of this link https://github.com/abpframework/abp/issues/1153 to insert data. I found also some people complaining about it https://github.com/aspnetboilerplate/aspnetboilerplate/issues/1437. The unit test code is not giving any error or log. Any help or steps would be appreciated.

Note: testing with 1 db-context is working properly

Thanks

Wello86 avatar Aug 04 '20 12:08 Wello86

Hi

  • Your ABP Framework version.
  • Exception message and stack trace if available (check the logs).
  • Steps needed to reproduce the problem.
  • Please provide a project if possible.

maliming avatar Aug 04 '20 12:08 maliming

Sorry,

Your ABP Framework version --> 2.3.0 Exception message and stack trace if available (check the logs) --> No logs written and no exceptions either Steps needed to reproduce the problem --> Just make the file "EntityFrameworkCoreTestModule" like attached in project "ProjectName.EntityFrameworkCore.Tests" then access the services or repositories which are using the second context

InspectionEntityFrameworkCoreTestModule.zip

Wello86 avatar Aug 04 '20 14:08 Wello86

hi Can you share a full source code of project?

maliming avatar Aug 05 '20 01:08 maliming

Hi, The solution itself is too big, so I uploaded test projects only if they will help. You can use this test class as beginning "test\Linkdev.Inspection.Application.Tests\UnitTesting\InspectedEntityAppService_Tests" as some other code was for just trials but not used anymore.

test.zip

Wello86 avatar Aug 05 '20 09:08 Wello86

hi @Wello86 You can use a template project. In this way I can easy debug it.

maliming avatar Aug 05 '20 09:08 maliming

Hi @maliming, Ok will use a template project then send you

Wello86 avatar Aug 05 '20 09:08 Wello86

Hi @maliming, when I was making a new sample for you on the new version, I discovered the issue and fixed it. It was related to UnitOfWorkManager. This link https://github.com/abpframework/abp/issues/1153 helped me a lot. Now I will test the part of accessing 2 databases in same Fact so still do not know if this is going to work or not. so insert data be like in version 3.0.1 public Task SeedAsync(DataSeedContext context) { /* Seed additional test data... */ using (var uow = _unitOfWorkManager.Begin(requiresNew: true)) { // insert data here } return Task.CompletedTask; } but in version 2.3.0 sometimes you need for this and "autoSave: true" and sometimes no. Not sure how this happens but anyway it is working now :) Thanks

Wello86 avatar Aug 06 '20 09:08 Wello86

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Oct 05 '20 10:10 stale[bot]

maybe there is a advice. https://blog.csdn.net/qq_29621089/article/details/110229881

ol964614088 avatar Nov 27 '20 06:11 ol964614088