Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4074376
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T17:09:37+00:00 2026-05-20T17:09:37+00:00

In the application I am testing I want to make sure that for certain

  • 0

In the application I am testing I want to make sure that for certain inputs the logger is being called. Some of the methods have different possible logger messages (ex. “value is malformed”, “value is out of range”). Thus, I want to make sure that the logger is being called with the correct message.

The logger strings themselved are in a RESX and may be localized in the future. Also, the wording may change. So simply comparing with a hard-coded string could result in breaking the associated tests whenever the text changes. Since they are localizable, this would mean I would have to force the unit-test to execute under a certain culture, or is there a better way?

Complicating this, the RESX strings are actually not as simple as above, but of the form "Line {0}: Value '{1}' at column {2} is malformed." This is then used as input to string.Format() to produce "Line 12: Value '12a.45' at column 45 is malformed.", for example. This is the string which the logger then actually gets. Should I hard-code this string into the unit-test?

Edit:

I’ll try to explain with some more detail, with a strongly simplified method to show what I mean:

public void ConvertSomething(object value)
{
    if (/* Check if valid value */)
    {
        var convertedValue = /* Some conversion */ ;
        if (/* Check is in range */)
        {
            // Do something
        }
        else
        {
            Logger.Log(string.Format(Resources.OutOfRange, LineNumber, convertedValue));
        }
    }
    else
    {
        Logger.Log(string.Format(Resources.InvalidValue, LineNumber, value, ColumnNumber));
    }
}

How do I test that the logger gets called with the correct message? I can’t simply check if any call to the logger was made. If I give it a valid, but out-of-range value, but the validity check has a bug, it will call the logger, but with the message “invalid value”, when it should actually be “out of range”.

I can mock the logger, so I can see that it got called and what the parameters were. But what should I assert against?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T17:09:39+00:00Added an answer on May 20, 2026 at 5:09 pm

    You can change the current culture of threads in code, if the culture exists on the machine. If it doesn’t exist, you need to install / create it.

    As for comparing the strings used in the application (that may change), I would suggest not to make that the target of the test.

    Instead, perhaps have a sanity test string in each resource that is static, and test for that. If that changes with the culture then you have loaded the correct resource and can safely assume the rest of the contents are correct also.

    NUnit supports changing the culture of test harnesses:

    http://www.nunit.org/index.php?p=culture&r=2.4.8

    Update: I see so the issue is the test cannot look into the logger? I would suggest that you can either mock a logger than knows it is being tested and contains its own assertions, or abandon testing the logger itself and test that satellite assemblies are loaded currently on a culture change. If the logger simply uses the resource file then the .NET runtime will handle the loading of the correct file or default to the base culture file.

    You can then have separate tests to show that the logger logs, without worrying about what it logs as that is covered in another test.

    Update 2: I think I see what you are trying to do. Would it be possible at all to have the logger accept the string format and a params object[] args in order to get access to the string before it is inserted with specific values?

    Alternatively, is it possible to make the string static or is it by nature changing each test run? It is valid for the test to have values hardcoded in them if the test itself is responsible for seeding the values in the first place. Also, as for the culture, I would address those changes separately to the changes introduced by different code paths choosing different messages.

    Update 3: sorry, I could not see the wood for the trees. The test itself can build the expected string from the resx file that the code uses – and just plugs in hardcoded values. This relies on the test output not differing from each run.

    If the resx changes, then so long as the number of values plugged into the formatted string doesn’t change, you shouldn’t need to fix either the code or the test.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When you guys are unit testing an application that relies on values from an
I am unit testing a .NET application (.exe) that uses an app.config file to
I am introducing automated integration testing to a mature application that until now has
Question How can I make sure my application is thread-safe? Are their any common
I'm currently looking into unit testing for a new application I have to create.
I am testing my Zend Framework application and would like to test that something
I'm building an application which I'm also testing in Heroku. I ran into some
I have begun doing dojo accessibility testing on a web application and have noticed
I am testing an application which communicates to a server via a UDP port,
I've been testing an application using my machine as a server, and everything's going

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.