.NET Core how to unit test service?

Viewed 13038

I have build a WebAPI and want to create a unit test project to have my services tested automatically.

The flow of my WebAPI is simple:

Controller (DI Service) -> Service (DI Repository) -> _repo CRUD

Suppose I have a service like:

public int Cancel(string id) //change status filed to 'n'
{
    var item = _repo.Find(id);
    item.status = "n";
    _repo.Update(item);
    return _repo.SaveChanges();
}

And I want to build a unit test, which just use InMemoryDatabase.

public void Cancel_StatusShouldBeN() //Testing Cancel() method of a service
{
    _service.Insert(item); 

    int rs = _service.Cancel(item.Id);
    Assert.Equal(1, rs);

    item = _service.GetByid(item.Id);
    Assert.Equal("n", item.status);
}

I've searched other related question, found that

You can't use dependency injections on test classes.

I just want to know if there is any other solution to achive my unit test idea?

1 Answers
Related