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

{

/// 

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

/// 

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());

serviceCollection.UseFlatwhiteFilters();

serviceCollection.RegisterWithMethodFilters(ServiceLifetime.Singleton);

serviceCollection.AddSingleton();

var sp = serviceCollection.BuildServiceProvider();

var instance = sp.GetRequiredService();

var proxy = sp.GetRequiredService();

// 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(() => instance.GetById(0));

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

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

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

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

instance.Delete(1);

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

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

await instance.DeleteAsync(1);

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

}

}

}

你可能感兴趣的:(Flatwhite,html,javascript,xhtml)