浏览代码

add and fixed some unit tests.

undefined
Savorboard 7 年前
父节点
当前提交
6238e78ae4
共有 5 个文件被更改,包括 126 次插入5 次删除
  1. +53
    -0
      test/DotNetCore.CAP.Test/CAP.BuilderTest.cs
  2. +4
    -5
      test/DotNetCore.CAP.Test/ConsumerInvokerFactoryTest.cs
  3. +1
    -0
      test/DotNetCore.CAP.Test/ConsumerServiceSelectorTest.cs
  4. +62
    -0
      test/DotNetCore.CAP.Test/JsonContentSerializerTest.cs
  5. +6
    -0
      test/DotNetCore.CAP.Test/QueueExecutorFactoryTest.cs

+ 53
- 0
test/DotNetCore.CAP.Test/CAP.BuilderTest.cs 查看文件

@@ -1,6 +1,8 @@
using System;
using System.Data;
using System.Threading.Tasks;
using DotNetCore.CAP.Abstractions;
using DotNetCore.CAP.Models;
using Microsoft.Extensions.DependencyInjection;
using Xunit;

@@ -49,6 +51,30 @@ namespace DotNetCore.CAP.Test
Assert.NotNull(thingy);
}

[Fact]
public void CanOverrideContentSerialize()
{
var services = new ServiceCollection();
services.AddCap(x => { }).AddContentSerializer<MyContentSerializer>();

var thingy = services.BuildServiceProvider()
.GetRequiredService<IContentSerializer>() as MyContentSerializer;

Assert.NotNull(thingy);
}

[Fact]
public void CanOverrideMessagePack()
{
var services = new ServiceCollection();
services.AddCap(x => { }).AddMessagePacker<MyMessagePacker>();

var thingy = services.BuildServiceProvider()
.GetRequiredService<IMessagePacker>() as MyMessagePacker;

Assert.NotNull(thingy);
}

[Fact]
public void CanResolveCapOptions()
{
@@ -59,6 +85,33 @@ namespace DotNetCore.CAP.Test
Assert.NotNull(capOptions);
}

private class MyMessagePacker : IMessagePacker
{
public string Pack(CapMessage obj)
{
throw new NotImplementedException();
}

public CapMessage UnPack(string packingMessage)
{
throw new NotImplementedException();
}
}


private class MyContentSerializer : IContentSerializer
{
public T DeSerialize<T>(string content)
{
throw new NotImplementedException();
}

public string Serialize<T>(T obj)
{
throw new NotImplementedException();
}
}

private class MyProducerService : ICapPublisher
{
public void Publish<T>(string name, T contentObj, string callbackName = null)


+ 4
- 5
test/DotNetCore.CAP.Test/ConsumerInvokerFactoryTest.cs 查看文件

@@ -18,6 +18,7 @@ namespace DotNetCore.CAP.Test
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>();
@@ -39,9 +40,7 @@ namespace DotNetCore.CAP.Test
};
var messageContext = new MessageContext();

var context = new ConsumerContext(description, messageContext);

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

Assert.NotNull(invoker);
}
@@ -63,11 +62,11 @@ namespace DotNetCore.CAP.Test

var context = new ConsumerContext(description, messageContext);

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

Assert.Throws<Exception>(() =>
{
invoker.InvokeAsync().GetAwaiter().GetResult();
invoker.InvokeAsync(context).GetAwaiter().GetResult();
});
}
}

+ 1
- 0
test/DotNetCore.CAP.Test/ConsumerServiceSelectorTest.cs 查看文件

@@ -1,6 +1,7 @@
using System;
using System.Threading.Tasks;
using DotNetCore.CAP.Abstractions;
using DotNetCore.CAP.Internal;
using Microsoft.Extensions.DependencyInjection;
using Xunit;



+ 62
- 0
test/DotNetCore.CAP.Test/JsonContentSerializerTest.cs 查看文件

@@ -0,0 +1,62 @@
using System;
using DotNetCore.CAP.Internal;
using DotNetCore.CAP.Models;
using Newtonsoft.Json;
using Xunit;

namespace DotNetCore.CAP.Test
{
public class JsonContentSerializerTest
{
[Fact]
public void CanSerialize()
{
//Arrange
var fixtrue = Create();

var message = new CapMessageDto
{
Id = "1",
Content = "Content",
CallbackName = "Callback",
Timestamp = DateTime.Now
};

//Act
var ret = fixtrue.Serialize(message);

//Assert
var expected = JsonConvert.SerializeObject(message);
Assert.NotNull(ret);
Assert.Equal(expected, ret);
}

[Fact]
public void CanDeSerialize()
{
//Arrange
var fixtrue = Create();

var message = new CapMessageDto
{
Id = "1",
Content = "Content",
CallbackName = "Callback",
Timestamp = DateTime.Now
};
var messageJson = JsonConvert.SerializeObject(message);

//Act
var ret = fixtrue.DeSerialize<CapMessageDto>(messageJson);

//Assert
Assert.NotNull(ret);
Assert.Equal(message.Id, ret.Id);
Assert.Equal(message.Content, ret.Content);
Assert.Equal(message.CallbackName, ret.CallbackName);
Assert.Equal(message.Timestamp, ret.Timestamp);
}

private JsonContentSerializer Create() => new JsonContentSerializer();
}
}

+ 6
- 0
test/DotNetCore.CAP.Test/QueueExecutorFactoryTest.cs 查看文件

@@ -1,6 +1,8 @@
using System;
using DotNetCore.CAP.Internal;
using Microsoft.Extensions.DependencyInjection;
using Xunit;
using Moq;

namespace DotNetCore.CAP.Test
{
@@ -13,6 +15,10 @@ namespace DotNetCore.CAP.Test
var services = new ServiceCollection();
services.AddLogging();
services.AddOptions();

var callbackSender = Mock.Of<ICallbackMessageSender>();

services.AddSingleton(callbackSender);
services.AddCap(x => { });
_provider = services.BuildServiceProvider();
}


正在加载...
取消
保存