Mocking IPrincipal in ASP.NET Core

The controller’s User is accessed through the HttpContext of the controller. The latter is stored within the ControllerContext. The easiest way to set the user is by assigning a different HttpContext with a constructed user. We can use DefaultHttpContext for this purpose, that way we don’t have to mock everything. Then we just use that … Read more

Test parameterization in xUnit.net similar to NUnit

xUnit offers a way to run parameterized tests through something called data theories. The concept is equivalent to the one found in NUnit but the functionality you get out of the box is not as complete. Here’s an example: [Theory] [InlineData(“Foo”)] [InlineData(9)] [InlineData(true)] public void Should_be_assigned_different_values(object value) { Assert.NotNull(value); } In this example xUnit will … Read more

Pass complex parameters to [Theory]

There are many xxxxData attributes in XUnit. Check out for example the MemberData attribute. You can implement a property that returns IEnumerable<object[]>. Each object[] that this method generates will be then “unpacked” as a parameters for a single call to your [Theory] method. See i.e. these examples from here Here are some examples, just for … Read more

xUnit.net: Global setup + teardown?

As far as I know, xUnit does not have a global initialization/teardown extension point. However, it is easy to create one. Just create a base test class that implements IDisposable and do your initialization in the constructor and your teardown in the IDisposable.Dispose method. This would look like this: public abstract class TestsBase : IDisposable … Read more

NUnit vs. MbUnit vs. MSTest vs. xUnit.net [closed]

I know this is an old thread, but I thought I’d post a vote for xUnit.NET. While most of the other testing frameworks mentioned are all pretty much the same, xUnit.NET has taken a pretty unique, modern, and flexible approach to unit testing. It changes terminology, so you no longer define TestFixtures and Tests…you specify … Read more