【问题标题】:Test my service with xUnit and asp.net core使用 xUnit 和 asp.net core 测试我的服务
【发布时间】:2016-12-07 20:25:31
【问题描述】:

我想用 asp.net core 和 xunit 测试我的服务。

这是我的服务:

public class MyService : IMyService
{
    public int Add(int x, int y)
    {
        return x + y;
    }
}

public interface IMyService
{
    int Add(int x, int y);
}

我在 startup.cs 中注册了我的服务

services.AddTransient<IMyService, MyService>();

但是现在我如何获取实例并在测试项目中测试我的服务?

到目前为止,在我的测试项目中,我有:

public class Class1
{
    IMyService myService;

    [Fact]
    public void PassingTest()
    {
        Assert.Equal(4, myService.Add(2,2));
    }
}

【问题讨论】:

  • 您到底想测试什么?
  • 我在 MyService 类中的方法
  • 然后新建一个实例并调用被测方法
  • 我已经用我的解决方案更新了我的帖子,但它不起作用

标签: c# unit-testing asp.net-core .net-core xunit


【解决方案1】:

新建一个实例并调用被测方法。没有必要把事情弄得太复杂。

public class Class1 { 

    IMyService myService = new MyService();

    [Fact]
    public void PassingTest()
    {
        Assert.Equal(4, myService.Add(2,2));
    }

}

【讨论】:

  • 我收到以下错误:“未处理的异常:System.IO.FileNotFoundException:无法加载文件或程序集'Microsoft.DotNet.InternalAbstractions,版本=1.0.0.0,文化=中性,PublicKeyToken =adb9793829ddae60'。系统找不到指定的文件。"
  • Workaround: add Microsoft.DotNet.InternalAbstractions to test dependencies manually - github.com/dotnet/core/issues/366
猜你喜欢
  • 2017-12-12
  • 2017-10-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多