vendredi 21 août 2015

Understanding Moq's Setup() function

I have some confusion with Setup().

From my understanding when we declare:

Mock<IUnitOfWork> uwork = new Mock<IUnitOfWork>();

We are creating a mock repository that will never actually reach-out to a database. Since it never touches the database we have to give it some mock data.

For example:

Question question = new Question {
    Title = "General question",
    Message = "Message body text..."
}

Here's where I'm a bit confused. From my understanding we are telling our Mocked repository what data to return and under which circumstance to return it.

                   // in this circumstance         // return this
uwork.Setup(i =. i.QuestionsRepository.GetById(1)).Returns(question)

At this point we create an instance of our controller and pass uwork.object to the controller instance. When the controller calls the (circumstance) method, our Mock repository produces the return value we specified.

Question

Is this correct? If not stop me here and correct me. If so, then why doesn't something like this work?

uwork.Setup(i => i.QuestionsRepository
        .GetAll().Where(l => l.Message_Id == 1).ToList())
        .Returns(questions); 
        // questions is a List<Question>

I get an exception:

An exception of type 'System.NotSupportedException' occurred in Moq.dll but was not handled in user code

Additional information: Expression references a method that does not belong to the mocked object: i => i.LegalInquiryRepository.GetAll().Where(l => l.legalCommunication_Id ==

Aucun commentaire:

Enregistrer un commentaire