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
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;
}
}
}
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));
}
}
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));
}
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);
}
}
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));
}
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") };
}
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.
Top comments (0)