Hi Maliming,
I have sent you a sample project with source code, showing the exception. Please let me know if you need anything else.
Thanks,
Jesse
Thanks Maliming.....I figured the issue out, and something wasn't being injected properly. It had nothing to do with ABP code.....
Thanks for your help.
Jesse
Hi Albert, yes Maliming was able to answer the question posed in the that support request along with this issue I am experiencing with this support request.
The ticket can be closed.
Thanks,
Jesse
Thanks Maliming! This helped alot, and fixed my issues....
Jesse
Thanks Maliming.... I am going to send you a test project shortly with details on what exactly is failing.
Thank you very much Ensin!
Jesse
Hi Albert,
The ABP Commercial version we are using is Version="4.4.2"
Please let me know if there is anything else you need.
Thanks,
Jesse
is it beta version?
Hi Albert,
No it is the commercial version from ABP...
Thanks,
Jesse
Hi Enisn,
Further to this question, how can I make the menu items I added invisible when the user is NOT logged in?
Thanks for your help!
Jesse
Hi Maliming,
I have a method in my razor.cs class :
protected virtual async Task SetPermissionsAsync()
{
HasEditPermission = await AuthorizationService.IsGrantedAsync(UpdatePolicyName);
}
This returns an IAuthorizationService, and comes from the static class AbpAuthorizationServiceExtensions. This method is first Initialized in the following start up method in the razor component:
protected override async Task OnInitializedAsync()
{
await SetPermissionsAsync();
}
This is where the error occurs. I need to pass an instance of the IAuthorizationService to the component during the test. Here is the test I wrote:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Bunit;
using Bunit.TestDoubles;
using Cao.CatOs.Blazor.Pages;
using Cao.CatOs.Blazor.Services;
using Microsoft.AspNetCore.Authorization;
using Microsoft.Extensions.DependencyInjection;
using NSubstitute;
using Volo.Abp.Authorization;
using Volo.Abp.LanguageManagement;
using Xunit;
namespace Cao.CatOs.Blazor.Tests
{
public class MultilingualComponent
{
protected string UpdatePolicyName = LanguageManagementPermissions.LanguageTexts.Edit;
[Fact]
public void Should_ReturnMultilingualExportHeader_ForRenderedComponent()
{
// Arrange
using var ctx = new TestContext();
var authContext = ctx.AddTestAuthorization();
authContext.SetAuthorized("admin");
//TODO:Mock parameters for CustomLanguageService using NSubstitute
var languageManagement = Substitute.For<ICustomLanguageTextAppService>();
var navigationManager = Substitute.For<INavigationService>();
var component = Substitute.For<CatOsComponentBase>();
ctx.Services.AddSingleton(component);
ctx.Services.AddSingleton(languageManagement);
ctx.Services.AddSingleton(navigationManager);
// Act
var cut = ctx.RenderComponent<Multilingual>();
// Assert
cut.MarkupMatches("<h3>Multilingual Export</h3>");
}
}
}
The error occurs on the //Act line . The exception is :
Volo.Abp.AbpException
authorizationService should implement Volo.Abp.Authorization.IAbpAuthorizationService
at Microsoft.AspNetCore.Authorization.AbpAuthorizationServiceExtensions.AsAbpAuthorizationService(IAuthorizationService authorizationService)
at Microsoft.AspNetCore.Authorization.AbpAuthorizationServiceExtensions.AuthorizeAsync(IAuthorizationService authorizationService, Object resource, String policyName)
at Microsoft.AspNetCore.Authorization.AbpAuthorizationServiceExtensions.AuthorizeAsync(IAuthorizationService authorizationService, String policyName)
at Microsoft.AspNetCore.Authorization.AbpAuthorizationServiceExtensions.IsGrantedAsync(IAuthorizationService authorizationService, String policyName)
at Cao.CatOs.Blazor.Pages.Multilingual.SetPermissionsAsync() in C:\CAT-OS\src\Cao.CatOs.Blazor\Pages\Multilingual.razor.cs:line 49
at Cao.CatOs.Blazor.Pages.Multilingual.OnInitializedAsync() in C:\CAT-OS\src\Cao.CatOs.Blazor\Pages\Multilingual.razor.cs:line 38
at Microsoft.AspNetCore.Components.ComponentBase.RunInitAndSetParametersAsync()
at Bunit.Rendering.TestRenderer.AssertNoUnhandledExceptions() in /_/src/bunit.core/Rendering/TestRenderer.cs:line 355
at Bunit.Rendering.TestRenderer.Render[TResult](RenderFragment renderFragment, Func`2 activator) in /_/src/bunit.core/Rendering/TestRenderer.cs:line 237
at Bunit.Rendering.TestRenderer.RenderFragment(RenderFragment renderFragment) in /_/src/bunit.core/Rendering/TestRenderer.cs:line 55
at Bunit.Extensions.TestContextBaseRenderExtensions.RenderInsideRenderTree(TestContextBase testContext, RenderFragment renderFragment) in /_/src/bunit.core/Extensions/TestContextBaseRenderExtensions.cs:line 45
at Bunit.Extensions.TestContextBaseRenderExtensions.RenderInsideRenderTree[TComponent](TestContextBase testContext, RenderFragment renderFragment) in /_/src/bunit.core/Extensions/TestContextBaseRenderExtensions.cs:line 25
at Bunit.TestContext.Render[TComponent](RenderFragment renderFragment) in /_/src/bunit.web/TestContext.cs:line 70
at Bunit.TestContext.RenderComponent[TComponent](ComponentParameter[] parameters) in /_/src/bunit.web/TestContext.cs:line 40
at Cao.CatOs.Blazor.Tests.MultilingualComponent.Should_ReturnMultilingualExportHeader_ForRenderedComponent() in C:\CAT-OS\Cao.CatOs.Blazor.Tests\MultilingualComponent.cs:line 38
The question I have is how do I inject / mock the object Volo.Abp.Authorization.IAbpAuthorizationService, so my test picks it up? Just to be clear I am using BUnit, and my razor component inherits from the AbpComponentBase. This is why when I run the page IABPAuthorizationService exists, however in a unit test it is null.
What I would like is an example of how to take a standard Razor componet that properly implements the ABP framework and write a unit test for it using BUnit. I am sure you will find there are quite a few items that need to be mocked and injected, in order to get it to work properly. Do you have an example I can use to follow along?
Thanks,
Jesse