Search code examples
c#xunitbunit

How to add test authorization with inherited TestContext in BUnit?


I have an inherited TestContext in BUnit and I want to add the Testauthorization.

This doesn't work:

using Bunit;
using Bunit.TestDoubles;
public class TestClass : TestContext
{
        [Fact]
        public void CompTest()
        {
            AddTestAuthorization();
        }
}

if I use the "normal" version with the boilerplate code like following it works.

This works:

using Bunit;
using Bunit.TestDoubles;
public class TestClass
{
        public void CompTest()
        {
            TestContext ctx = new TestContext();
            ctx.AddTestAuthorization();
        }
}

So my question is, how to add test authorization with inherited TestContext in BUnit?


Solution

  • Since AddTestAuthorization is an extension method, you need to use this to get to it. Its an unfortunate limitation in the C# language.

    E.g.:

    using Bunit;
    using Bunit.TestDoubles;
    public class TestClass : TestContext
    {
            [Fact]
            public void CompTest()
            {
                this.AddTestAuthorization();
            }
    }