Hello guys, I tried a lot of different approaches, however I am unable to create a new LocalPlayer and, therefore, get a LocalPlayerSubsystem for unit testing.
Here’s a sample of code:
constexpr auto InventoryModifierTestFlags = EAutomationTestFlags::EngineFilter | EAutomationTestFlags::EditorContext |
EAutomationTestFlags::HighPriority;
IMPLEMENT_SIMPLE_AUTOMATION_TEST(FPolkerCore_Inventory_CollectionsShouldBeLoaded,
"PolkerCore.Inventory.CollectionsShouldBeLoaded",
InventoryModifierTestFlags)
bool FPolkerCore_Inventory_CollectionsShouldBeLoaded::RunTest(const FString& Parameters)
{
UWorld* MyWorld = FAutomationEditorCommonUtils::CreateNewMap();
if (MyWorld)
{
UGameInstance* MyGameInstance = NewObject<UGameInstance>(MyWorld, UGameInstance::StaticClass());
UEngine* MyEngine = GEngine;
if (MyEngine)
{
MyWorld->SetGameInstance(MyGameInstance);
MyGameInstance->InitializeStandalone();
FString Error;
ULocalPlayer* MyLocalPlayer = MyGameInstance->CreateLocalPlayer(0, Error, true);
UPlayerInventorySubsystem* PlayerInventorySubsystem = ULocalPlayer::GetSubsystem<UPlayerInventorySubsystem>(MyLocalPlayer);
TestNotNull(TEXT("PlayerInventorySubsystem is valid"), PlayerInventorySubsystem);
TestTrue(TEXT("Dealer Collection count should be greater than 0"), PlayerInventorySubsystem->DealersCollection.Num() > 0);
TestTrue(TEXT("Accessories Collection count should be greater than 0"), PlayerInventorySubsystem->AccessoriesCollection.Num() > 0);
}
else
{
TestFalse(TEXT("Engine is invalid"), true);
}
}
else
{
TestFalse(TEXT("World is invalid"), true);
}
return true;
}
Debug mode stucks at the following point:
bool UGameInstance::IsDedicatedServerInstance() const
{
if (IsRunningDedicatedServer())
{
return true;
}
else
{
return WorldContext ? WorldContext->RunAsDedicated : false; //<--
}
}
Not sure how to proceed after that, tried a lot of different approaches and none worked.
Thank you beforehand for help and hopefully the answer may help other users!
Rafael