I'd like to Mock the getReviews
method of the CommentModel
so I can test if it's 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 when invoked 1 time(s).
Method was expected to be called 1 times, actually called 0 times.
Please why is the method not called?
You're doing it backwards.
You need to first define your mock's expectation, then pass it to your controller so it's being called and the expectation met.
You are having your mock being called first by your controller's constructor and then define an expectation to be called which never happens.