目录
[1 TestFilterOnProxyWithoutTarget](#1 TestFilterOnProxyWithoutTarget)
[1.1 Test_ex_thrown_from_filters](#1.1 Test_ex_thrown_from_filters)
[1.2 Test_catching_ex_thrown_from_filters](#1.2 Test_catching_ex_thrown_from_filters)
[1.2.1 // Exceptions are handled](#1.2.1 // Exceptions are handled)
- TestFilterOnProxyWithoutTarget
using Flatwhite.Core.Tests.Attributes;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Moq;
using System;
using System.Threading.Tasks;
using Xunit;
namespace Flatwhite.Core.Tests
{
public class TestFilterOnProxyWithoutTarget
{
-
- Test_ex_thrown_from_filters
[Fact]
public async Task Test_ex_thrown_from_filters()
{
var serviceCollection = new ServiceCollection();
serviceCollection.UseFlatwhiteFilters();
serviceCollection.AddSingleton(Mock.Of<ILogger>());
serviceCollection.AddProxyWithoutTarget<IProductService>(ServiceLifetime.Singleton);
var sp = serviceCollection.BuildServiceProvider();
var proxy = sp.GetRequiredService<IProductService>();
var ex = Assert.Throws<Exception>(() => proxy.Delete(1));
Assert.Equal($"{nameof(BadMethodFilterAttribute)}.{nameof(BadMethodFilterAttribute.OnMethodExecuting)}", ex.Message);
ex = await Assert.ThrowsAsync<Exception>(() => proxy.DeleteAsync(1));
Assert.Equal($"{nameof(BadMethodFilterAttribute)}.{nameof(BadMethodFilterAttribute.OnMethodExecutingAsync)}", ex.Message);
}
-
- Test_catching_ex_thrown_from_filters
[Fact]
public async Task Test_catching_ex_thrown_from_filters()
{
var serviceCollection = new ServiceCollection();
serviceCollection.UseFlatwhiteFilters();
serviceCollection.AddProxyWithoutTarget<IProductService>(ServiceLifetime.Singleton);
var sp = serviceCollection.BuildServiceProvider();
var proxy = sp.GetRequiredService<IProductService>();
-
-
- // Exceptions are handled
-
proxy.DeleteBySku(Guid.NewGuid());
await proxy.DeleteBySkuAsync(Guid.NewGuid());
}
}
}