Selaa lähdekoodia

add ConsumerInvokerFactory unit test.

master
yangxiaodong 7 vuotta sitten
vanhempi
commit
a162d4884b
2 muutettua tiedostoa jossa 84 lisäystä ja 0 poistoa
  1. +72
    -0
      test/DotNetCore.CAP.Test/ConsumerInvokerFactoryTest.cs
  2. +12
    -0
      test/DotNetCore.CAP.Test/Sample.cs

+ 72
- 0
test/DotNetCore.CAP.Test/ConsumerInvokerFactoryTest.cs Näytä tiedosto

@@ -0,0 +1,72 @@
using System;
using System.Linq;
using System.Reflection;
using DotNetCore.CAP.Abstractions;
using DotNetCore.CAP.Internal;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Xunit;

namespace DotNetCore.CAP.Test
{
public class ConsumerInvokerFactoryTest
{
IConsumerInvokerFactory consumerInvokerFactory;

public ConsumerInvokerFactoryTest()
{
var services = new ServiceCollection();
services.AddLogging();
var provider = services.BuildServiceProvider();
var logFactory = provider.GetRequiredService<ILoggerFactory>();
var binder = new ModelBinderFactory();

consumerInvokerFactory = new ConsumerInvokerFactory(logFactory, binder, provider);
}

[Fact]
public void CreateInvokerTest()
{
var methodInfo = typeof(Sample).GetRuntimeMethods()
.Single(x => x.Name == nameof(Sample.ThrowException));

var description = new ConsumerExecutorDescriptor
{
MethodInfo = methodInfo,
ImplTypeInfo = typeof(Sample).GetTypeInfo()
};
var messageContext = new MessageContext();

var context = new ConsumerContext(description, messageContext);

var invoker = consumerInvokerFactory.CreateInvoker(context);

Assert.NotNull(invoker);
}

[Theory]
[InlineData(nameof(Sample.ThrowException))]
[InlineData(nameof(Sample.AsyncMethod))]
public async void InvokeMethodTest(string methodName)
{
var methodInfo = typeof(Sample).GetRuntimeMethods()
.Single(x => x.Name == methodName);

var description = new ConsumerExecutorDescriptor
{
MethodInfo = methodInfo,
ImplTypeInfo = typeof(Sample).GetTypeInfo()
};
var messageContext = new MessageContext();

var context = new ConsumerContext(description, messageContext);

var invoker = consumerInvokerFactory.CreateInvoker(context);

await Assert.ThrowsAsync(typeof(Exception), async () =>
{
await invoker.InvokeAsync();
});
}
}
}

+ 12
- 0
test/DotNetCore.CAP.Test/Sample.cs Näytä tiedosto

@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace DotNetCore.CAP.Test
{
@@ -36,6 +37,17 @@ namespace DotNetCore.CAP.Test
{

}

public void ThrowException()
{
throw new Exception();
}

public async Task<int> AsyncMethod()
{
await Task.FromResult(3);
throw new Exception();
}
}

public class ComplexType


Ladataan…
Peruuta
Tallenna