Wednesday, March 15, 2017

Laravel phpunit test issue offsetExists() does not exist on this mock object

I'm having an issue with phpunit and mockery to test my laravel controller.It always returning an error of:

BadMethodCallException: Method Mockery_4_Illuminate_Http_Request::offsetExists() does not exist on this mock object

here's my controller:

class ImageController extends Controller{
    protected $imageRepository;
    protected $authorizationKey;

    public function __contruct(ImageRepository $imageRepository){
        $this->imageRepository = $imageRepository;
    }

    public function getImage(Request $request){
        //i think error comes on this line
        $this->authorizationKey = (new AuthKey())->getAuthKey($request->token);

        $images = $this->imageRepository->getByCategory($request->image_category, $request->limit);

        return $images;
    }
}

and here's my test:

public function test_images_get(){
    $imageRepo = Mockery::mock(ImageRepository::class);

    $request = Mockery::mock(Request::class, function($request){
        $request->shouldReceive('offsetGet')->with('limit')->andReturn(10);
        $request->shouldReceive('offsetGet')->with('image_category')->andReturn('Houses');
        $request->shouldReceive('offsetGet')->with('token')->andReturn('234asjkdhf293');
    });

    $imageController = new ImageController($imageRepo);
    $response = $imageController->getImage($request);
}

Anyone have an idea to fix? Thanks in advance.



via Cris

Advertisement