Mocking derived class with abstract base class

Hi, I have the following setup:
**BASE CLASS**
public abstract class SomeBaseAbstractClass<TItem, TResponse>
{
protected EctocloudCosmosRepository(IMapper mapper, IRepository<TItem> repository, ILogger logger)
{
_mapper = mapper;
_repository = repository;
_logger = logger;
}

public async Task<TResponse> GetAsync(string id, CancellationToken token)
{
// some logic removed for brevity
}
}

**IMPLEMENTED CLASS**
public class Repository : SomeBaseAbstractClass<Item, Response>
{
// ctor
// methods
}

**CONTROLLER**
public async Task<ActionResult> GetSomeStuff(
[FromRoute] string id,
[FromServices] Repository repository)
{
var response = await repository.GetAsync(id, HttpContext.RequestAborted);
return HandleResponse(response);
}
**BASE CLASS**
public abstract class SomeBaseAbstractClass<TItem, TResponse>
{
protected EctocloudCosmosRepository(IMapper mapper, IRepository<TItem> repository, ILogger logger)
{
_mapper = mapper;
_repository = repository;
_logger = logger;
}

public async Task<TResponse> GetAsync(string id, CancellationToken token)
{
// some logic removed for brevity
}
}

**IMPLEMENTED CLASS**
public class Repository : SomeBaseAbstractClass<Item, Response>
{
// ctor
// methods
}

**CONTROLLER**
public async Task<ActionResult> GetSomeStuff(
[FromRoute] string id,
[FromServices] Repository repository)
{
var response = await repository.GetAsync(id, HttpContext.RequestAborted);
return HandleResponse(response);
}
Now I would like to unit test GetSomeStuff, and ensuring correct HTTP status codes are returned based on response from .GetAsync() method. How would I unit test this?
2 Replies
pip
pip3y ago
that wouldn't be a unit test afaik, that's technically an end-to-end test. or implementation test
ConfusedPenguin
ConfusedPenguinOP3y ago
I would of course mock away all dependencies. I would only like to ensure that depending on what the answer is from
await repository.GetAsync(...)
await repository.GetAsync(...)
the correct HTTP response is returned.
Want results from more Discord servers?
Add your server