Add `UnitOfWorkExtensions_Tests`

Resolve #12260
pull/12263/head
maliming 3 years ago
parent bf49145716
commit 69b41a2db5
No known key found for this signature in database
GPG Key ID: 096224957E51C89E

@ -34,7 +34,7 @@ public static class UnitOfWorkExtensions
{
Check.NotNull(unitOfWork, nameof(unitOfWork));
return unitOfWork.Items.FirstOrDefault(x => x.Key == key).As<TValue>();
return unitOfWork.Items.FirstOrDefault(x => x.Key == key).Value.As<TValue>();
}
public static TValue GetOrAddItem<TValue>([NotNull] this IUnitOfWork unitOfWork, string key, Func<string, TValue> factory)

@ -0,0 +1,58 @@
using Shouldly;
using Volo.Abp.Testing;
using Xunit;
namespace Volo.Abp.Uow;
public class UnitOfWorkExtensions_Tests : AbpIntegratedTest<AbpUnitOfWorkModule>
{
private readonly IUnitOfWorkManager _unitOfWorkManager;
public UnitOfWorkExtensions_Tests()
{
_unitOfWorkManager = GetRequiredService<IUnitOfWorkManager>();
}
[Fact]
public void AddItem()
{
var uow = _unitOfWorkManager.Begin();
uow.AddItem("testKey", "testValue");
uow.Items.ShouldContainKey("testKey");
uow.Items.ContainsValue("testValue");
}
[Fact]
public void GetItemOrDefault()
{
var uow = _unitOfWorkManager.Begin();
uow.Items.Add("testKey", new NameValue("TestKey","TestValue"));
uow.GetItemOrDefault<NameValue>("testKey").ShouldBeOfType<NameValue>();
uow.GetItemOrDefault<NameValue>("testKey").Value.ShouldBe("TestValue");
}
[Fact]
public void GetOrAddItem()
{
var uow = _unitOfWorkManager.Begin();
var item = uow.GetOrAddItem("testKey", _ => new NameValue("TestKey", "TestValue"));
item.Name.ShouldBe("TestKey");
item.ShouldBeOfType<NameValue>();
item.Value.ShouldBe("TestValue");
}
[Fact]
public void RemoveItem()
{
var uow = _unitOfWorkManager.Begin();
uow.Items.Add("testKey", "testValue");
uow.RemoveItem("testKey");
uow.Items.ShouldNotContainKey("testKey");
}
}
Loading…
Cancel
Save