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 Datadog

How to Diagram Your Cloud Architecture

Cloud architecture diagrams provide critical visibility into the resources in your environment and how they’re connected. In our latest eBook, AWS Solution Architects Jason Mimick and James Wenzel walk through best practices on how to build effective and professional diagrams.

Download the Free eBook

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

AWS Security LIVE!

Hosted by security experts, AWS Security LIVE! showcases AWS Partners tackling real-world security challenges. Join live and get your security questions answered.

Tune in to the full event

DEV is partnering to bring live events to the community. Join us or dismiss this billboard if you're not interested. ❤️