Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IFeatureManager Unit Testing

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.

like image 486
Roberto Alessi Avatar asked May 26 '26 23:05

Roberto Alessi


1 Answers

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"));
}
like image 178
Alexander Avatar answered May 28 '26 11:05

Alexander



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!