Base solution for your next web application
Open Closed

dynamic localization and unit testing - can we get extra parameters back also? #8742


User avatar
0
carelearning created

Hello Volosoft,

We have the need to have localization of error messages. For example, "TenantId is missing" or "ConnectionString is missing." To accomplish this we have added this to AbpZeroTemplate.xml: <text name="CustomNotFound">{0} is missing</text> That works great.

Then our code will return such errors like: var errormessage = L("CustomNotFound", nameof(tenantId)); or var errormessage = L("CustomNotFound", nameof(connectionId));

This also works great.

Then we go to test it. So we have a test to look for "TenantId is missing" or "ConnectionString is missing" like this

     [Fact]
    public async Task AddCoursesAsync_NullTenantId_ReturnsFailure()
    {
        const string ErrorMessage = "tenantId is missing.";
        var tenantId = null;
        var actual = await _sut.Validate(tenantId, _connectionString);

        actual.Error.ShouldBe(ErrorMessage);
    }
      [Fact]
    public async Task AddCoursesAsync_NullConnectionString_ReturnsFailure()
    {
        const string ErrorMessage = "connectionString is missing.";
        var connectionString = null;
        var actual = await _sut.Validate(_tenantId, connectionString);

        actual.Error.ShouldBe(ErrorMessage);
    }

Note that we are using: _sut.LocalizationManager = NullLocalizationManager.Instance; per this post: https://support.aspnetzero.com/QA/Questions/1836

So it will return the error of "CustomNotFound." In this case, we need it to be unique. It cannot be "CustomNotFound" for both cases. Is it possible to get back something like "CustomNotFound, tenantId" or something like that to make it unique? In this module alone we have 114 unit tests based on this. We would like to avoid creating a localization for each one.

Update after response from @ismcagdas: Thank you for your response. Since we are using the "L()" method, it appears that I would have to create my own LocalizationSource.GetString(name, args) this is in LocalizationSourceExtensions.cs. Do I create another version of LocalizationSourceExtensions as well? or can I create that in my own NullLocalizationSource.cs? Or is this a better way?

Thanks again for your help.


3 Answer(s)