Browse Source

modify unit tests.

master
Savorboard 7 years ago
parent
commit
3003d94584
1 changed files with 28 additions and 21 deletions
  1. +28
    -21
      test/DotNetCore.CAP.Test/ConsumerInvokerFactoryTest.cs

+ 28
- 21
test/DotNetCore.CAP.Test/ConsumerInvokerFactoryTest.cs View File

@@ -4,44 +4,48 @@ using System.Reflection;
using DotNetCore.CAP.Abstractions; using DotNetCore.CAP.Abstractions;
using DotNetCore.CAP.Internal; using DotNetCore.CAP.Internal;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Moq;
using Xunit; using Xunit;


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

private Mock<IContentSerializer> _mockSerialiser;
private Mock<IMessagePacker> _mockMessagePacker;
private Mock<IModelBinderFactory> _mockModelBinderFactory;


public ConsumerInvokerFactoryTest() public ConsumerInvokerFactoryTest()
{ {
_mockSerialiser = new Mock<IContentSerializer>();
_mockMessagePacker = new Mock<IMessagePacker>();
_mockModelBinderFactory = new Mock<IModelBinderFactory>();

var services = new ServiceCollection(); var services = new ServiceCollection();
services.AddLogging();
services.AddSingleton<IContentSerializer, JsonContentSerializer>();
services.AddSingleton<IMessagePacker, DefaultMessagePacker>();
var provider = services.BuildServiceProvider();
var logFactory = provider.GetRequiredService<ILoggerFactory>();
var mesagePacker = provider.GetRequiredService<IMessagePacker>();
var binder = new ModelBinderFactory();
services.AddSingleton<ConsumerInvokerFactory>();


consumerInvokerFactory = new ConsumerInvokerFactory(logFactory, mesagePacker,binder, provider);
services.AddLogging();
services.AddSingleton(_mockSerialiser.Object);
services.AddSingleton(_mockMessagePacker.Object);
services.AddSingleton(_mockModelBinderFactory.Object);
_serviceProvider = services.BuildServiceProvider();
} }


private ConsumerInvokerFactory Create() =>
_serviceProvider.GetService<ConsumerInvokerFactory>();

[Fact] [Fact]
public void CreateInvokerTest() 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();
// Arrange
var fixure = Create();


var invoker = consumerInvokerFactory.CreateInvoker();
// Act
var invoker = fixure.CreateInvoker();


// Assert
Assert.NotNull(invoker); Assert.NotNull(invoker);
} }


@@ -50,6 +54,9 @@ namespace DotNetCore.CAP.Test
[InlineData(nameof(Sample.AsyncMethod))] [InlineData(nameof(Sample.AsyncMethod))]
public void InvokeMethodTest(string methodName) public void InvokeMethodTest(string methodName)
{ {
// Arrange
var fixure = Create();

var methodInfo = typeof(Sample).GetRuntimeMethods() var methodInfo = typeof(Sample).GetRuntimeMethods()
.Single(x => x.Name == methodName); .Single(x => x.Name == methodName);


@@ -62,7 +69,7 @@ namespace DotNetCore.CAP.Test


var context = new ConsumerContext(description, messageContext); var context = new ConsumerContext(description, messageContext);


var invoker = consumerInvokerFactory.CreateInvoker();
var invoker = fixure.CreateInvoker();


Assert.Throws<Exception>(() => Assert.Throws<Exception>(() =>
{ {


Loading…
Cancel
Save