Base solution for your next web application
Starts in:
01 DAYS
01 HRS
01 MIN
01 SEC
Open Closed

AbpSettings in xUnit Test #5031


User avatar
0
oscargonzalezmoreno created

Hello, I'm trying to obtain a Setting inside a xUnit Test.

Via GetSettingValueForTenant, the system says that no Setting for this "name" exist. Buy If I get an GetAllSettingValuesForTenant, I can see the value... There is a snippet:


string name = "Project.bd22b07c-a0f1-4cd5-b759-a054ba60673b.SdFileWriterChannel.FileRepo";

// HERE I GOT AN ERROR var test = _settingManager.GetSettingValueForTenant(name, tenantId.GetValueOrDefault());

// BUT NOT HERE... IReadOnlyList<ISettingValue> settings = _settingManager.GetAllSettingValuesForTenant (tenantId.GetValueOrDefault()); foreach (var item in settings) { if(item.Name.Equals(name)) { return item.Value; } }

Am I missing something?

Kind regards, Oscar


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

    Hi @oscargonzalezmoreno,

    Can you also share your setting definition ?

  • User Avatar
    0
    oscargonzalezmoreno created

    Hello, I'm not using Setting Definition, I'm just pre-inserting the data in the database. Maybe it's the problem, but as I said, I don't understand how I can get the Setting value with the second method...

  • User Avatar
    0
    aaron created
    Support Team

    ABP is open source, so you can check the implementation in SettingManager.cs.

    GetSettingValueForTenant calls _settingDefinitionManager.GetSettingDefinition(name), which does:

    _settings.TryGetValue(name, out settingDefinition)
    

    On the other hand, GetAllSettingValuesForTenant calls SettingStore.GetAllListAsync(tenantId, null), which does:

    _settingRepository.GetAllListAsync(s => s.UserId == userId && s.TenantId == tenantId)
    

    So, these are for different purposes:

    • The first gets the predefined setting, even if not overridden for the specified tenant.
    • The second gets all settings from DB, but only if overridden for the specified tenant.

    If you insist on not defining settings, the second way is an undocumented (non-guaranteed) way of getting the setting value.