Activities of "PDarioJer"

  • ABP Framework version: v8.2.0
  • UI Type: Blazor Server
  • Database System: EF Core (SQL Server)
  • Tiered (for MVC) or Auth Server Separated (for Angular): no
  • Exception message and full stack trace:
Autofac.Core.Registration.ComponentNotRegisteredException: The requested service 'Volo.Abp.AspNetCore.Components.Web.AbpBlazorMessageLocalizerHelper`1...

Autofac.Core.Registration.ComponentNotRegisteredException
The requested service 'Volo.Abp.AspNetCore.Components.Web.AbpBlazorMessageLocalizerHelper`1[[DummyPlatform.Localization.DummyPlatformResource, DummyPlatform.Domain.Shared, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]' has not been registered. To avoid this exception, either register a component to provide the service, check for service registration using IsRegistered(), or use the ResolveOptional() method to resolve an optional dependency.

See https://autofac.rtfd.io/help/service-not-registered for more info.
   at Autofac.ResolutionExtensions.ResolveService(IComponentContext context, Service service, IEnumerable`1 parameters)
   at Autofac.ResolutionExtensions.Resolve(IComponentContext context, Type serviceType, IEnumerable`1 parameters)
   at Autofac.ResolutionExtensions.Resolve(IComponentContext context, Type serviceType)
   at Autofac.Extensions.DependencyInjection.AutofacServiceProvider.GetRequiredService(Type serviceType)
   at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider, Type serviceType)
   at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService[T](IServiceProvider provider)
   at Volo.Abp.AbpTestBaseWithServiceProvider.GetRequiredService[T]()
   at DummyPlatform.Blazor.Tests.Items.ItemPage_IntegrationShould.Load_Items() in C:\Users\user\source\repos\DummyPlatform\test\DummyPlatform.Blazor.Tests\Items\ItemPage_IntegrationShould.cs:line 22
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor)
   at System.Reflection.MethodBaseInvoker.InvokeWithNoArgs(Object obj, BindingFlags invokeAttr)
  • Steps to reproduce the issue:
  • I followed the guide in Ma Liming's blogpost to create a test suite: https://community.abp.io/posts/how-to-test-blazor-components-in-abp-phcijx8e
  • However, it seems to have an issue with the LocalizerHelper. I tried to get the required service in my test but it didn't seem to work

Item.razor

@page "/items"
@using DummyPlatform.Items
@using DummyPlatform.Localization
@using Volo.Abp.AspNetCore.Components.Web
@inherits DummyPlatformComponentBase
@inject IItemAppService ItemAppService
@inject AbpBlazorMessageLocalizerHelper<DummyPlatformResource> LH
<Card>
    <CardHeader>
        <Row Class="justify-content-between">
            <Column ColumnSize="ColumnSize.IsAuto">
                <h2>@L["Items"]</h2>
            </Column>
        </Row>
    </CardHeader>
    <CardBody>
        <DataGrid TItem="ItemDto"
                  Data="ItemList"
                  ReadData="OnDataGridReadAsync"
                  TotalItems="TotalCount"
                  ShowPager="true"
                  PageSize="PageSize">
            <DataGridColumns>
                <DataGridColumn TItem="ItemDto"
                                Field="@nameof(ItemDto.ItemNr)"
                                Caption="@L["ItemNr"]"></DataGridColumn>
                <DataGridColumn TItem="ItemDto"
                                Field="@nameof(ItemDto.VariantCode)"
                                Caption="@L["VariantCode"]"></DataGridColumn>
                <DataGridColumn TItem="ItemDto"
                                Field="@nameof(ItemDto.Description)"
                                Caption="@L["Description"]"></DataGridColumn>
            </DataGridColumns>
        </DataGrid>
    </CardBody>
</Card>

<Modal @ref="ItemDetailModal">
    <_ModalBackdrop></_ModalBackdrop>
    <ModalContent IsCentered="true">
        <Form>
            <ModalHeader>
                <ModalTitle>@OpenedItem.ItemNr</ModalTitle>
                <CloseButton Clicked="CloseItemDetailModal"></CloseButton>
            </ModalHeader>
            <ModalBody>
                <Field>
                    <FieldLabel>@L["ItemNr"]</FieldLabel>
                    <TextEdit @bind-Text="@OpenedItem.ItemNr"></TextEdit>
                </Field>
                <Field>
                    <FieldLabel>@L["VariantCode"]</FieldLabel>
                    <TextEdit @bind-Text="@OpenedItem.ItemNr"></TextEdit>
                </Field>
                <Field>
                    <FieldLabel>@L["Description"]</FieldLabel>
                    <TextEdit @bind-Text="@OpenedItem.Description"></TextEdit>
                </Field>
            </ModalBody>
            <ModalFooter>
                <Button Color="Color.Secondary"
                        Clicked="CloseItemDetailModal">
                    @L["Cancel"]
                </Button>
            </ModalFooter>
        </Form>
    </ModalContent>
</Modal>

Unit Test

using DummyPlatform.Items;
using DummyPlatform.Localization;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Localization;
using NSubstitute;
using Volo.Abp.AspNetCore.Components.Web;
using Xunit;

namespace DummyPlatform.Blazor.Tests.Items;

public class ItemPage_IntegrationShould : DummyPlatformBlazorTestBase
{
	private IItemAppService _itemAppService;
	private AbpBlazorMessageLocalizerHelper<DummyPlatformResource> _lh;
	
	[Fact]
	public void Load_Items()
	{
		// Arrange
		var ctx = CreateTestContext();
		_itemAppService = Substitute.For<IItemAppService>();
		_lh = GetRequiredService<AbpBlazorMessageLocalizerHelper<DummyPlatformResource>>();
		
		ctx.Services.AddSingleton(_itemAppService);
		ctx.Services.AddSingleton(_lh);
		
		// Act
		ctx.RenderComponent<DummyPlatform.Blazor.Pages.Items>();
		
		// Assert
		_itemAppService.Received(1).GetListAsync(Arg.Any<GetItemListDto>());
	}
}

I kept the Testbase the same as in the blogpost. As you can see, I kept very close to the Tutorial from the ABP documentation, so someone else could encounter the same problem. Do I maybe have a missing dependency here?

When I call GuidGenerator.Create() in my DomainService, that I instantiated in a test, I get a NullReferenceException. I think I could solve this by implementing the IDomainService instead and then mock the IGuidGenerator interface. However, there really is no need to do that here aside from testing concerns.

Do you have any suggestions on how to workaround this issue?

(ItemManager.cs)

using System.Collections.Generic;
using System.Threading.Tasks;
using Volo.Abp.Domain.Services;

namespace TestPlatform.Items;
public class ItemManager : DomainService
{
	private readonly IItemRepository _itemRepository;

	public ItemManager(IItemRepository itemRepository)
	{
		_itemRepository = itemRepository;
	}

	public async Task<Item> CreateAsync(
		string itemNr,
		int variantCode,
		string description,
	)
	{
		var existingItem = await _itemRepository.FindByItemNrAndVariantAsync(itemNr, variantCode);
		if (existingItem != null)
		{
			throw new ItemAlreadyExistsException(itemNr, variantCode);
		}

		var newGuid = GuidGenerator.Create();
		return new Item(
			newGuid,
			itemNr,
			variantCode,
			description,
		);
	}
}

(ItemManagerTest.cs)

using System.Threading.Tasks;
using TestPlatform.Items.Shared;
using NSubstitute;
using NSubstitute.ReturnsExtensions;
using Shouldly;
using Xunit;

namespace TestPlatform.Items;
public class ItemManager_CreateAsyncShould
{
	private IItemRepository itemRepository = Substitute.For<IItemRepository>();

	[Fact]
	public async Task Return_Item_If_No_Item_With_Same_ItemNr_Exists()
	{
		itemRepository.FindByItemNrAndVariantAsync(
			ValidItemData.ItemNr,
			ValidItemData.VariantCode
		).ReturnsNull();
		var itemManager = new ItemManager(itemRepository);

		var item = await itemManager.CreateAsync(
			ValidItemData.ItemNr,
			ValidItemData.VariantCode,
			ValidItemData.Description,
		);

		item.ShouldBeOfType<Item>();
		item.ShouldNotBeNull();
	}
}
  • ABP Framework version: v8.1.3
  • UI Type: Blazor Server
  • Database System: EF Core (SQL Server)
  • Tiered: no
  • Exception message and full stack trace: System.NullReferenceException : Object reference not set to an instance of an object.
  • ItemManager.CreateAsync(String itemNr, Int32 variantCode, String description) Zeile 27 ItemManager_CreateAsyncShould.Return_Item_If_No_Item_With_Same_ItemNr_Exists() Zeile 22 --- End of stack trace from previous location ---
  • Steps to reproduce the issue: Call GuidGenerator.Create() from within a testing context

Edit: For some reason, markdown removes everything inside the <> arrows. However, the content is not important for the problem at hand anyway.

Showing 1 to 2 of 2 entries
Made with ❤️ on ABP v9.0.0-preview Updated on September 19, 2024, 10:13