TestAttributeFilters

目录

1 IProductService

1.1 /// This is the interface without any implementation

2 TestAttributeFilters

2.1 Test_interceptors

  1. IProductService

using Flatwhite.Core.Tests.Attributes;

using System;

using System.Threading.Tasks;

namespace Flatwhite.Core.Tests

{

/// <summary>

    1. /// This is the interface without any implementation

/// </summary>

public interface IProductService

{

[BadMethodFilter]

void Delete(int id);

[BadMethodFilter]

Task DeleteAsync(int id);

[BadMethodFilter]

[HandleAllMethodExceptions]

void DeleteBySku(Guid sku);

[BadMethodFilter]

[HandleAllMethodExceptions]

Task DeleteBySkuAsync(Guid sku);

}

}

  1. TestAttributeFilters

using System;

using System.Threading.Tasks;

using Flatwhite.Core.Tests.Attributes;

using Microsoft.Extensions.DependencyInjection;

using Microsoft.Extensions.Logging;

using Moq;

using Xunit;

namespace Flatwhite.Core.Tests

{

public class TestAttributeFilters

{

    1. Test_interceptors

[Fact]

public async Task Test_interceptors()

{

var serviceCollection = new ServiceCollection();

serviceCollection.AddSingleton(Mock.Of<ILogger>());

serviceCollection.UseFlatwhiteFilters();

serviceCollection.RegisterWithMethodFilters<IOrderService, OrderService>(ServiceLifetime.Singleton);

serviceCollection.AddSingleton<FilterAttributeOnClassMethod>();

var sp = serviceCollection.BuildServiceProvider();

var instance = sp.GetRequiredService<OrderService>();

var proxy = sp.GetRequiredService<IOrderService>();

// All exceptions are handled if calling proxy

Assert.Null(proxy.GetById(0));

Assert.Null(await proxy.GetByIdAsync(0));

Assert.Equal(1, proxy.GetById(1).Id);

Assert.Equal(1, (await proxy.GetByIdAsync(1)).Id);

proxy.Delete(0);

proxy.Delete(1);

proxy.Delete(2000);

await proxy.DeleteAsync(0);

await proxy.DeleteAsync(1);

await proxy.DeleteAsync(2000);

// Exception is not handled by default if calling instance

Assert.Throws<ArgumentException>(() => instance.GetById(0));

await Assert.ThrowsAsync<ArgumentException>(() => instance.GetByIdAsync(0));

Assert.Equal(1, instance.GetById(1).Id);

Assert.Equal(1, (await instance.GetByIdAsync(1)).Id);

Assert.Throws<ArgumentException>(() => instance.Delete(0));

instance.Delete(1);

Assert.Throws<InvalidOperationException>(() => instance.Delete(2000));

await Assert.ThrowsAsync<ArgumentException>(() => instance.DeleteAsync(0));

await instance.DeleteAsync(1);

await Assert.ThrowsAsync<InvalidOperationException>(() => instance.DeleteAsync(2000));

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

m0_74456535

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值