Open Closed

How-to-bypass-the-RequiresFeature-attribute-temporarily-when-running-unit-tests #9220


User avatar
0
raif created
  • Exception message and full stack trace: Message:  Volo.Abp.Authorization.AbpAuthorizationException : Exception of type 'Volo.Abp.Authorization.AbpAuthorizationException' was thrown.

Stack Trace:  FeatureCheckerExtensions.CheckEnabledAsync(IFeatureChecker featureChecker, Boolean requiresAll, String[] featureNames) MethodInvocationFeatureCheckerService.CheckAsync(MethodInvocationFeatureCheckerContext context) FeatureInterceptor.CheckFeaturesAsync(IAbpMethodInvocation invocation) FeatureInterceptor.InterceptAsync(IAbpMethodInvocation invocation) CastleAsyncAbpInterceptorAdapter1.InterceptAsync(IInvocation invocation, IInvocationProceedInfo proceedInfo, Func3 proceed) AsyncInterceptorBase.ProceedAsynchronous(IInvocation invocation, IInvocationProceedInfo proceedInfo) CastleAbpMethodInvocationAdapter.ProceedAsync() ValidationInterceptor.InterceptAsync(IAbpMethodInvocation invocation) CastleAsyncAbpInterceptorAdapter1.InterceptAsync(IInvocation invocation, IInvocationProceedInfo proceedInfo, Func3 proceed) AsyncInterceptorBase.ProceedAsynchronous(IInvocation invocation, IInvocationProceedInfo proceedInfo) CastleAbpMethodInvocationAdapter.ProceedAsync() UnitOfWorkInterceptor.InterceptAsync(IAbpMethodInvocation invocation) CastleAsyncAbpInterceptorAdapter1.InterceptAsync(IInvocation invocation, IInvocationProceedInfo proceedInfo, Func3 proceed)

  • Steps to reproduce the issue:

Suppose we are trying to create a unit/integration test for a method in an application service layer equipped with a [RequiresFeature] attribute

[RequiresFeature(MyFeatures.MyAwesomeNewFeature)]
public virtual async Task< ItemsDto > GetListOfItemsAsync()
{
	return new ItemsDto
	{
		....
	};
}

Already in the ApplicationTestModule module with the AddAlwaysAllowAuthorization() extension all Permissions are being bypassed. But how can we bypass feature requirement for test purposes ?

public class IMyApplicationTestModule : AbpModule
{
    public override void ConfigureServices(ServiceConfigurationContext context)
    {
        context.Services.AddAlwaysAllowAuthorization();
        ....
    }
}

I tried to mock IFeatureChecker like in following example. But it broke all other unit test under MyAppService_Tests

    public class MyAppService_Tests : IMyApplicationTestBase
    {
        private readonly IMyAppService _iMyAppService;
        private readonly MyOptions _myOptions;
        private readonly ISettingProvider _settingProvider;

        private IFeatureChecker _featureChecker;

        public IMyAppService_Tests()
        {
            _iMyAppService = GetRequiredService< IMyAppService >();
            _myOptions = GetRequiredService< IOptions < MyOptions > >().Value;
            _settingProvider = GetRequiredService< ISettingProvider >();
        }

        protected override void AfterAddApplication(IServiceCollection services)
        {
            _featureChecker = Substitute.For< IFeatureChecker >();
            _featureChecker.IsEnabledAsync(MyFeatures.MyAwesomeNewFeature).Returns(Task.FromResult(true));
            services.AddSingleton(_featureChecker);
        }
        

        [Fact]
        public async Task GetListOfItems_Should_Return_IItemsDto()
        {
            // Arrange
            ....
            // Act
            var result = await _iMyAppService.GetListOfItemsAsync();

            // Assert
            result.ShouldNotBeNull();
        }}

This usage going to throw following exeception ex;

Volo.Abp.AbpInitializationException : An error occurred during the initialize Volo.Abp.Modularity.OnApplicationInitializationModuleLifecycleContributor phase of the module MyCompany.MyTag.MyApp.IdentityTestBaseModule, MyCompany.MyTag.MyApp.TestBase, Version=0.1.0.0, Culture=neutral, PublicKeyToken=null: The input string '' was not in a correct format.. See the inner exception for details.
---- System.FormatException : The input string '' was not in a correct format.

1 Answer(s)
  • User Avatar
    0
    berkansasmaz created
    Support Team .NET Developer

    Hi Raif,

    You can mock the IFeatureStore service instead IFeatureChecker. (https://github.com/abpframework/abp/blob/dev/framework/test/Volo.Abp.Features.Tests/Volo/Abp/Features/TestFeatureStore.cs#L9)

Boost Your Development
ABP Live Training
Packages
See Trainings
Mastering ABP Framework Book
Do you need assistance from an ABP expert?
Schedule a Meeting
Mastering ABP Framework Book
The Official Guide
Mastering
ABP Framework
Learn More
Mastering ABP Framework Book
Made with ❤️ on ABP v9.3.0-preview. Updated on May 12, 2025, 05:22