简体   繁体   中英

Test my service with xUnit and asp.net core

I would like to test my service with asp.net core and xunit.

Here's my service:

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

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

I registered my service in startup.cs

services.AddTransient<IMyService, MyService>();

But now how can I get an instance and test my service in test project?

So far in my test project I have:

public class Class1
{
    IMyService myService;

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

New up an instance and call the method under test. No need to over complicate things.

public class Class1 { 

    IMyService myService = new MyService();

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

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM