mardi 2 juin 2015

How can mock dependencies to Web API controller unit test?

In my current MVC application, I have architected a series of command objects to handle business actions. These business actions would be wrapped around service endpoints. These endpoints would also be consumed by an MVC frond-end & a windows app. Every business action will call into a DAO action, which in turn, calls into the required data access repositories to successfully perform the business action. I have listed an example action below.

Busines Action

public class CreateProjectAction
{
    IInsertProjectDAOAction InsertProjectDAOAction { get; set; }

    public void Execute()
    {
        // Does some business validation & other logic before
        //  calling the DAO action
        InsertProjectDAOAction.Execute();
    }
}

DAO Action

public interface IInsertProjectDAOAction
{
    void Execute();
}

public class InsertProjectDAOAction
{
    IProjectRepository ProjectRepository { get; set; }

    public void Execute()
    {
        ProjectRepository.Insert();
    }
}

Project Repository

public interface IProjectRepository 
{
    void Insert(Project proj);

    // other db methods would be listed here
}

public class ProjectRepository
{
    public void Insert(Project proj)
    {
        // Insert into the data store
    }
}

I am using Ninject to assist with the dependency injection between layers. I have a bunch of unit tests around the business 'CreateProjectAction' to test out expected behavior of that object. The business actions are wrapped around a series of Web API service endpoints. I would also like to write tests around my MVC controllers so that I can be sure they work as planned.

I like the architecure so far, but having trouble figuring out how to mock the DAO action properties in the business action when writing unit tests for the mvc controller. I'd love to hear suggestions, other viewpoints, etc ...

Aucun commentaire:

Enregistrer un commentaire