Thank you, this helped!
Unfortunately, the GuidGenerator is a protected property, so I won't be able to set it manually. Getting the ItemManager through DI is an option, however, I don't want it to use the actual ItemRepository but a Substitute, so I can return a fake return value on the FindByItemNrAndVariantAsync(...) method.
My question here is: Is there a way to provide the domain service with an implementation of the GuidGenerator without using the IDomainService interface? Or alternatively, is there a way to get the ItemManager through dependency injection but with a mocked ItemRepository?
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();
}
}
Edit: For some reason, markdown removes everything inside the <> arrows. However, the content is not important for the problem at hand anyway.