DEV Community

Kenichiro Nakamura
Kenichiro Nakamura

Posted on

4 2

C#: Test multiple Exception with xUnit and moq

In this week, I had an opportunity to write unit test code to test a method which returns different type of Exception based on condition. I could write multiple [fact] but to make it simple, I used [theory] to cover all exception combinations.

Create Sample App

Let's create simple app. As I just want to illustrate what I have done, I add just one project.

1. Create xunit project and add nuget.

dotnet new xunit -n myexceptiontest
dotnet add .\myexceptiontest\myexceptiontest.csproj package moq
start .\myexceptiontest\myexceptiontest.csproj
Enter fullscreen mode Exit fullscreen mode

2. Add new service class which throws exception with some condition.

// MyService.cs
using System;

namespace myexceptiontest;

public interface IMyService
{
    public int MyMethod(int input);
}

public class MyService : IMyService
{
    public int MyMethod(int input)
    {
        switch (input)
        {
            case 1:
                throw new ArgumentException();
            case 2:
                throw new DivideByZeroException();
            case 3:
                throw new InvalidCastException();
            default:
                return input * 2;
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Unit test for Service

Write Fact unit test

First, I add [Fact] test in UnitTest1.cs file.

using Moq;
using System;
using Xunit;

namespace myexceptiontest;

public class UnitTest1
{
    [Fact]
    public void Test1()
    {
        // arrange
        int input = 10;
        MyService myService = new MyService();

        // act
        int result = myService.MyMethod(input);
        Assert.Equal(20, result);
    }

    [Fact]
    public void TestException1()
    {
        // arrange
        int input = 1;
        MyService myService = new MyService();

        // act and assert
        Exception ex = Assert.Throws<ArgumentException>(()=> myService.MyMethod(input));
    }

    [Fact]
    public void TestException2()
    {
        // arrange
        int input = 2;
        MyService myService = new MyService();

        // act and assert
        Exception ex = Assert.Throws<DivideByZeroException>(() => myService.MyMethod(input));
    }
}
Enter fullscreen mode Exit fullscreen mode

Use theory for multiple exception test

Now I use [Theory] to support some scenario.

[Theory]
[InlineData(1, typeof(ArgumentException))]
[InlineData(2, typeof(DivideByZeroException))]
[InlineData(3, typeof(InvalidCastException))]
public void TestExceptions(int input, Type exceptionType)
{
    // arrange
    MyService myService = new MyService();

    // act and assert
    Exception ex = Assert.Throws(exceptionType, ()=> myService.MyMethod(input));
}
Enter fullscreen mode Exit fullscreen mode

Unit Test for Class

Obviously, we have class to consume the service. Let's add it now.

namespace myexceptiontest;

public class MyClass
{
    private readonly IMyService myService;

    public MyClass(IMyService myService)
    {
        this.myService = myService;
    }

    public int Do(int input)
    {
        return this.myService.MyMethod(input);
    }
}
Enter fullscreen mode Exit fullscreen mode

Write Fact unit test

I add [Fact] test first. I use mock to control MyService behavior.

[Fact]
public void MyClassException1()
{
    // arrange
    Mock<IMyService> mockedIMyService = new Mock<IMyService>();
    mockedIMyService.Setup(x=>x.MyMethod(It.IsAny<int>())).Throws<ArgumentException>();
    MyClass myClass = new MyClass(mockedIMyService.Object);

    // act and assert
    Exception ex = Assert.Throws<ArgumentException>(() => myClass.Do(1));
}

[Fact]
public void MyClassException2()
{
    // arrange
    Mock<IMyService> mockedIMyService = new Mock<IMyService>();
    mockedIMyService.Setup(x => x.MyMethod(It.IsAny<int>())).Throws<DivideByZeroException>();
    MyClass myClass = new MyClass(mockedIMyService.Object);

    // act and assert
    Exception ex = Assert.Throws<DivideByZeroException>(() => myClass.Do(1));
}
Enter fullscreen mode Exit fullscreen mode

Use theory for multiple exception test

To throw particular exception instance with moq, we need to pass an instance of the type. But InlineData won't accept instance data. Thus I used MemberData instead by which I can pass instances.

[Theory]
[MemberData(nameof(InputAndException))]
public void MyClassExceptions(int input, Exception exception)
{
    // arrange
    Mock<IMyService> mockedIMyService = new Mock<IMyService>();
    mockedIMyService.Setup(x => x.MyMethod(input)).Throws(exception);
    MyClass myClass = new MyClass(mockedIMyService.Object);

    // act and assert
    Exception ex = Assert.Throws(exception.GetType(), () => myClass.Do(input));
}

private static IEnumerable<object[]> InputAndException()
{
    yield return new object[] { 1, new ArgumentException("dummy") };
    yield return new object[] { 2, new DivideByZeroException("dummy") };
    yield return new object[] { 3, new InvalidCastException("dummy") };
}
Enter fullscreen mode Exit fullscreen mode

Summary

xUnit's theory provides several ways to test with input data, which is great.

There may be better way to achieve the same, so should you have any ideas, please let me know.

Image of AssemblyAI tool

Challenge Submission: SpeechCraft - AI-Powered Speech Analysis for Better Communication

SpeechCraft is an advanced real-time speech analytics platform that transforms spoken words into actionable insights. Using cutting-edge AI technology from AssemblyAI, it provides instant transcription while analyzing multiple dimensions of speech performance.

Read full post

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

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay