Home > OS >  IFeatureManager Unit Testing
IFeatureManager Unit Testing

Time:09-22

I have a Feature Flag in the Azure portal, used from some controllers in a .NET Core Web App.

At runtime, it works correctly switching on and off the FF on the real portal.

I should write 2 Unit tests, simulating when the Feature Flag is On and when Off.

For the Off, I can write

var featMan = new Mock<IFeatureManager>().Object;

And it works, the problem is to simulate when On.

I found this page, https://github.com/microsoft/FeatureManagement-Dotnet/issues/19#issue-517953297 , but in the downloadable code there is no StubFeatureManagerWithFeatureAOn definition.

CodePudding user response:

You just need to configure your Mock to return specific value in specific cases. For example to emulate that test-feature is On you'd write something like this

[Test]
public async Task TestFeatureManager()
{
    var featureManageMock = new Mock<IFeatureManager>();
    featureManageMock
        .Setup(m => m.IsEnabledAsync("test-feature"))
        .Returns(Task.FromResult(true));

    var featureManager = featureManageMock.Object;

    Assert.IsTrue(await featureManager.IsEnabledAsync("test-feature"));
}
  • Related