© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•4y ago•
4 replies
Marante

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?
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

❔ base class/abstract for record
C#CC# / help
3y ago
❔ Creating a base abstract class to ensure a set of derived classes receive similar dependencies?
C#CC# / help
3y ago
❔ Bind derived class and base class to CollectionView and Picker
C#CC# / help
3y ago
❔ How to instantiate derived class when base class requires args?
C#CC# / help
3y ago