0

I use NSubstitute for my NUnit tests and try to check if a method is called with the right values. Until know everything works fine. I have to localize the texts and so use resource strings for this. Every unit test now fails, that tests for a method to be recieved, where a string parameter contains a new line.

Here is a simplified example:

// Old Version, where the unit test succeed
public void CallWithText(ICallable callable)
{
    callable.ShowText("Some text with a new\nline."); 
}

// New Version, where the unit test fails
// Text of `Properties.Resources.TextWithNewLine` in the
// Resources.resx is "Some text with a new
// line."
public void CallWithText(ICallable callable)
{
    callable.ShowText(Properties.Resources.TextWithNewLine); 
}

[Test]
public void CallWithText_WhenCalled_CallsCallable()
{
    var caller = new Caller();
    var callable = Substitute.For<ICallable>();

    caller.CallWithText(callable);

    callable.Received(1).ShowText("Some text with a new\nline.");
}

It seems for me, that there is a problem with the new line. Does anybody has a solution, because it is a mess to adapt all the unit tests.

scher
  • 1,813
  • 2
  • 18
  • 39
  • Please post an [mcve]. Also post the output of the test that fails. Are you sure the resource file only contains a single newline and not a linefeed as well? – Lasse V. Karlsen Aug 11 '16 at 09:10
  • You have been right (like @Alexandr Niktin), the resource file contained \r\n and thats the reason the test failed. – scher Aug 11 '16 at 09:22

3 Answers3

2

Comparing exact strings in the unit test will add more maintaining work.
In your case new line can be different in different execution environment.

For strings I suggest asserting passed parameter with Contains method. Where you can check for the more important words

[Test]
public void CallWithText_WhenCalled_CallsCallable()
{
    var caller = new Caller();
    var callable = Substitute.For<ICallable>();

    caller.CallWithText(callable);

    callable.Received(1).ShowText(Arg.Is<string>(text => text.Contains("Some text")));
}
Fabio
  • 31,528
  • 4
  • 33
  • 72
2

The problem isn't related to NSubstitute but to String itself. The new line symbol isn't just \n. It's environment specific: \r\n on Windows, \n on Unix, \r on early Mac OSes. Please use Shift+Enter to add new lines properly in Resource manager.

You have a couple of ways to use:

  1. Environment.NewLine property which knows the new line symbol for the current environment:

    callable.Received(1).ShowText("Some text with a new" + Environment.NewLine + "line.");
    
  2. Use explicit new line in your expected string:

    callable.Received(1).ShowText(@"Some text with a new
    line.");
    
Alexandr Nikitin
  • 7,258
  • 2
  • 34
  • 42
1

This indicates the Properties.Resources.TextWithNewLine and "Some text with a new\nline." and different. Without knowing too much about Properties.Resources.TextWithNewLine, I suggest you try change the test to

[Test]
public void CallWithText_WhenCalled_CallsCallable()
{
    var caller = new Caller();
    var callable = Substitute.For<ICallable>();

    caller.CallWithText(callable);

    callable.Received(1).ShowText(Arg.Any<string>());
}

Or use the actual string if you really want to assert the content of the string, change to (make sure the resources file is in the test project)

[Test]
public void CallWithText_WhenCalled_CallsCallable()
{
    var caller = new Caller();
    var callable = Substitute.For<ICallable>();

    caller.CallWithText(callable);

    callable.Received(1).ShowText(Properties.Resources.TextWithNewLine);
}
Stephen Zeng
  • 2,748
  • 21
  • 18
  • Thanx for your answer. As the the example is simplified, the value of parameter in the origional example is composite. With the test I also want to test, that the value of the paramter is calculated correctly. So both of your succestions does not work in my case. – scher Aug 11 '16 at 08:24
  • @scher can you provide more information in OP to reflect this? – Stephen Zeng Aug 11 '16 at 08:30
  • Thanks for your work, sorry about my shortcoming explaining the question in detail. – scher Aug 11 '16 at 09:25