mercredi 31 août 2016

How do I Mock Methods in a Dependency Class of a Controller

Hello I'll like to Mock the getReviews method of the CommentModel so I can test if its called in the ApiReviewCommentsController Method.

This is my method

class ApiReviewCommentsController extends ApiController
{
    private $commentsModel;

    public function __construct(CommentsModel $commentsModel)
    {
        $this->commentsModel = $commentsModel;
        $this->commentsModel->getReviewComment();

    }
}

This is my test

public function testThatItShouldAddGetAllCommentsForReviewId(){


    $reviewId = 1;


    $commentsModel = $this->getMockBuilder(CommentsModel::class)->getMock();

    $controller = new ApiReviewCommentsController($commentsModel);

    $commentsModel->expects($this->once())
        ->method('getReviewComments')
        ->willReturn(false);

}

This is my error

Expectation failed for method name is equal to <string:getReviewComments> when invoked 1 time(s).
Method was expected to be called 1 times, actually called 0 times.

Please why is the method not called?

Aucun commentaire:

Enregistrer un commentaire