When I run the Should_CopyTemplate_Test() test case below, the reference to CurrentTenant in the method it calls is returning NullReferenceException, as if the ICurrentTenant is not being injected.
I have this test set up:
{
private ISltnRepository _sltnRepository;
private ICurrentTenant _currentTenant;
public SltnManager_Tests()
{
_sltnRepository = GetRequiredService<ISltnRepository>();
_currentTenant = GetRequiredService<ICurrentTenant>();
}
[Fact]
public async Task Should_CopyTemplate_Test()
{
// Arrange
var sltnManager = new SltnManager(_sltnRepository);
// Act
Guid sltnId = Guid.Parse("00000000-14ba-4f0f-8417-c2f312768aea");
var sltn = await sltnManager.CopyTemplate(sltnId);
//Assert
sltn.Name.ShouldBe("");
}
}
Testing this method:
public async Task<Sltn> CopyTemplate(Guid templateId)
{
// Get the template solution from the host tenant
Sltn template;
using (CurrentTenant.Change(null))
{
template = await _sltnRepository.GetAsync(templateId);
}
// set a branches list from the template
List<Branch> branches = new List<Branch>();
foreach(var templateBranch in template.Branches)
{
branches.Add(new Branch(GuidGenerator.Create(), templateBranch.Name, templateBranch.Description, templateBranch.AppUserId, templateBranch.ParentBranchId));
}
//create a new solution for the tenant from the template
var sltn = new Sltn(GuidGenerator.Create(), template.Name, template.Description, template.DisplayOrder, branches);
await _sltnRepository.InsertAsync(sltn);
return sltn;
}
However CurrentTenant.Change is returning NullReferenceException on CurrentTenant.
8 Answer(s)
-
0
// Arrange var sltnManager = new SltnManager(_sltnRepository);
Why not get service through DI container? eg:
GetRequiredService<SltnManager>()
-
0
Hi.
Thanks, that fixed the issue.
A supplementary question, does the SQlite EF Core Context used by the test framework support the IncludeDetails=true.
In my code above foreach(var templateBranch in template.Branches), Branches returns null.
Branches are added in a seeding class, which worked for SQL Server.
-
0
hi
Have you configured the
Include
extension in your EF Core module?https://github.com/abpframework/abp/blob/dev/modules/identity/src/Volo.Abp.Identity.EntityFrameworkCore/Volo/Abp/Identity/EntityFrameworkCore/IdentityEfCoreQueryableExtensions.cs#L8 https://github.com/abpframework/abp/blob/dev/modules/identity/src/Volo.Abp.Identity.EntityFrameworkCore/Volo/Abp/Identity/EntityFrameworkCore/EfCoreIdentityUserRepository.cs#L281
-
0
-
0
Does that not work for Sqlite?
Yes, Can you try same way just like the Identity module?
-
0
Ok.
I made the change and it worked.
Just to clarify.
- I only need to add the EfCoreQuerableExtensions because EF is using SQLite in Test mode ?
- Or, EfCoreQuerableExtensions need to be configured for SQL Server in Production as well ?
Thanks - Adam
-
0
hi
You always need to use
EfCoreQueryableExtensions
, This is recommended.It's related to EF Core, will be works for any provider, eg: sql server, sqlite,mysql.
-
0
Ok, Thanks very much for your help.