瀏覽代碼

Add Unit Tests.

master
yangxiaodong 7 年之前
父節點
當前提交
4d5af5db35
共有 1 個文件被更改,包括 92 次插入0 次删除
  1. +92
    -0
      test/DotNetCore.CAP.Test/ConsumerServiceSelectorTest.cs

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

@@ -0,0 +1,92 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using DotNetCore.CAP.Abstractions;
using DotNetCore.CAP.Internal;
using Microsoft.Extensions.DependencyInjection;
using Xunit;

namespace DotNetCore.CAP.Test
{
public class ConsumerServiceSelectorTest
{

private IServiceProvider _provider;

public ConsumerServiceSelectorTest()
{
var services = new ServiceCollection();
//services.AddSingleton<IConsumerServiceSelector, DefaultConsumerServiceSelector>();
services.AddScoped<IFooTest, CandidatesFooTest>();
services.AddScoped<IBarTest, CandidatesBarTest>();
services.AddLogging();
services.AddCap();
_provider = services.BuildServiceProvider();
}

[Fact]
public void CanFindAllConsumerService()
{
var selector = _provider.GetRequiredService<IConsumerServiceSelector>();
var candidates = selector.SelectCandidates(_provider);

Assert.Equal(2, candidates.Count);
}

[Fact]
public void CanFindSpecifiedTopic()
{
var selector = _provider.GetRequiredService<IConsumerServiceSelector>();
var candidates = selector.SelectCandidates(_provider);
var bestCandidates = selector.SelectBestCandidate("Candidates.Foo", candidates);

Assert.NotNull(bestCandidates);
Assert.NotNull(bestCandidates.MethodInfo);
Assert.Equal(bestCandidates.MethodInfo.ReturnType, typeof(Task));
}
}

public class CandidatesTopic : TopicAttribute
{
public CandidatesTopic(string topicName) : base(topicName)
{
}
}

public interface IFooTest { }
public interface IBarTest { }

public class CandidatesFooTest : IFooTest, IConsumerService
{
[CandidatesTopic("Candidates.Foo")]
public Task GetFoo()
{
Console.WriteLine("GetFoo() method has bee excuted.");
return Task.CompletedTask;
}

[CandidatesTopic("Candidates.Foo2")]
public void GetFoo2()
{
Console.WriteLine("GetFoo2() method has bee excuted.");
}
}

public class CandidatesBarTest : IBarTest
{
[CandidatesTopic("Candidates.Bar")]
public Task GetBar()
{
Console.WriteLine("GetBar() method has bee excuted.");
return Task.CompletedTask;
}

[CandidatesTopic("Candidates.Bar2")]
public void GetBar2()
{
Console.WriteLine("GetBar2() method has bee excuted.");
}
}

}

Loading…
取消
儲存