DEV Community

Cover image for Tests Everywhere - .NET C#
Roger Viñas Alcon
Roger Viñas Alcon

Posted on • Edited on

Tests Everywhere - .NET C#

GitHub logo rogervinas / tests-everywhere

🤠 Tests, Tests Everywhere!

.NET C# testing this simple Hello World with NUnit and Moq

Show me the code

Implementation

  1. Create HelloMessage interface and HelloWorldMessage implementing it in HelloMessage.cs:
public interface HelloMessage
{
  public String Text {
    get;
  }
}

public class HelloWorldMessage : HelloMessage
{
  public string Text {
    get {
      return "Hello World!";
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

Creating it as an interface will allow us to mock it for testing using Moq which does not support mocking final classes. Maybe other libraries support that but using an interface is simpler.

  1. Same way create HelloConsole interface and HelloSystemConsole class implementing it in HelloConsole.cs:
public interface HelloConsole {
  void Print(String text);
}

public class HelloSystemConsole : HelloConsole
{
  public void Print(String text) {
    Console.WriteLine(text);
  }
}
Enter fullscreen mode Exit fullscreen mode
  1. Create HelloApp in HelloApp.cs:
public class HelloApp
{
  private HelloMessage message;
  private HelloConsole console;

  public HelloApp(HelloMessage message, HelloConsole console) {
    this.message = message;
    this.console = console;
  }

  public void PrintHello() {
    console.Print(message.Text);
  }
}
Enter fullscreen mode Exit fullscreen mode
  1. Create main Hello.Main/Program.cs that wraps it all together:
var message = new HelloWorldMessage();
var console = new HelloSystemConsole();
var app = new HelloApp(message, console);
app.PrintHello();
Enter fullscreen mode Exit fullscreen mode

Test

Following NUnit > Writing Tests and Moq guides ...

  1. Test HelloMessage in HelloMessageTest.cs:
[Test]
public void ShouldReturnHelloWorld()
{
  var message = new HelloWorldMessage();
  Assert.That(message.Text, Is.EqualTo("Hello World!"));
}
Enter fullscreen mode Exit fullscreen mode
  1. Test HelloApp in HelloAppTest.cs:
[Test]
public void ShouldPrintHelloMessage()
{
  var messageText = "Hello Test!";

  // 2.1 Create a mock of HelloMessage
  var messageMock = new Mock<HelloMessage>();
  // 2.2 Expect HelloMessage mock to receive a call to Text
  // and return "Hello Test!"
  messageMock.Setup(message => message.Text).Returns(messageText);
  // 2.3 Get the mock object to pass it to HelloApp
  var message = messageMock.Object;

  // 2.4 Create a mock of HelloConsole
  var consoleMock = new Mock<HelloConsole>();
  // 2.5 No need to set expectations for this one
  // 2.6 Get the mock object to pass it to HelloApp
  var console = consoleMock.Object;

  // 2.3 Create a HelloApp, the one we want to test, passing the mocks
  var app = new HelloApp(message, console);
  // 2.4 Execute the method we want to test
  app.PrintHello();

  // 2.5 Verify HelloConsole mock Print() method
  // has been called once with "Hello Test!"
  consoleMock.Verify(console => console.Print(messageText), Times.Once);
}
Enter fullscreen mode Exit fullscreen mode
  1. Test output should look like:
NUnit Adapter 4.2.0.0: Test execution complete
  Passed ShouldPrintHelloMessage [180 ms]
  Passed ShouldReturnHelloWorld [7 ms]

Test Run Successful.
Total tests: 2
     Passed: 2
 Total time: 2.7702 Seconds
Enter fullscreen mode Exit fullscreen mode

Happy Testing! 💙

Image of Timescale

Timescale – the developer's data platform for modern apps, built on PostgreSQL

Timescale Cloud is PostgreSQL optimized for speed, scale, and performance. Over 3 million IoT, AI, crypto, and dev tool apps are powered by Timescale. Try it free today! No credit card required.

Try free

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Immerse yourself in a wealth of knowledge with this piece, supported by the inclusive DEV Community—every developer, no matter where they are in their journey, is invited to contribute to our collective wisdom.

A simple “thank you” goes a long way—express your gratitude below in the comments!

Gathering insights enriches our journey on DEV and fortifies our community ties. Did you find this article valuable? Taking a moment to thank the author can have a significant impact.

Okay