mardi 16 août 2016

How do I use Moq to mock an interface that has methods that take concrete classes as parameters?

  1. I have this scenario: an interface with 2 methods
  2. the 2 methods take requests and return response
  3. Methods contain functionality inside (check permissions and validate request and get data from database using entity framework.
  4. But I want to test the methods and not just the interface.
  5. I've tested the interface successfully but now I want to enter the method and test inside it.

Code example:

 public interface IMyInterface
    {
      [OperationContract]
      responseObject GetData(Service<RequestObject> request);
    }

    public class MyConcreteClass : IMyInterface
    {
       public responseObject GetData(Service<RequestObject> request)
       {
          CheckForNull(request);
          ValidateMethod(request);
          //connect to db
          using(var context = new contextEntity)
          {
            //get data
          }
       }
    }

Now, I want to test the check nulls, permissions and data access, is it possible? Or do I have to extract interface from the internal methods?

PS, this is for my unit testing. I'm trying to eliminate external dependencies. please explain in detail

Aucun commentaire:

Enregistrer un commentaire