- 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, Func
3 proceed)
AsyncInterceptorBase.ProceedAsynchronous(IInvocation invocation, IInvocationProceedInfo proceedInfo)
CastleAbpMethodInvocationAdapter.ProceedAsync()
ValidationInterceptor.InterceptAsync(IAbpMethodInvocation invocation)
CastleAsyncAbpInterceptorAdapter1.InterceptAsync(IInvocation invocation, IInvocationProceedInfo proceedInfo, Func
3 proceed)
AsyncInterceptorBase.ProceedAsynchronous(IInvocation invocation, IInvocationProceedInfo proceedInfo)
CastleAbpMethodInvocationAdapter.ProceedAsync()
UnitOfWorkInterceptor.InterceptAsync(IAbpMethodInvocation invocation)
CastleAsyncAbpInterceptorAdapter1.InterceptAsync(IInvocation invocation, IInvocationProceedInfo proceedInfo, Func
3 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)
-
0
Hi Raif,
You can mock the
IFeatureStore
service insteadIFeatureChecker
. (https://github.com/abpframework/abp/blob/dev/framework/test/Volo.Abp.Features.Tests/Volo/Abp/Features/TestFeatureStore.cs#L9)