vendredi 23 février 2018

Verify that a mock object was called with specific parameters [duplicate]

This question already has an answer here:

I am using the Laravel browserkit test to test a feature of my site but I have encountered the following problem.

The feature allows users to upload images to the site, the images are then uploaded to S3 with the filename having been changed to a hash and the entry is also added to the database to associate the user with the image.

Because uploading to S3 is expensive timewise and also costs I am mocking the uploader (to simplify things lets assume it's just a class with a single method called uploadFile).

$mock = $this->getMockForAbstractClass(Uploader::class);
$mock->expects($this->exactly(1))
    ->method("uploadFile")
    ->willReturn(true);
$this->app->instance(Uploader::class, $mock);
//1x1 png file for testing
$file = [ "fileMock" => new UploadedFile(public_path("img/1x1.png"), "1x1.png",  mime("png"), filesize(public_path("img/1x1.png")), null, true) ]; 
$request = [
    "submit" => 1,
    "title" => "mock title"
];
$this->call("POST", action("UploadController@addFile"), $request, [], $file);
$this->assertResponseStatus(302); //Will redirect back
$newFile = FilesTable->latest()->first(); //Last uploaded file
$this->assertEquals("mock title", $newFile->title);

This works, but ideally I also want to verify that the parameters passed to uploadFile match the filename that is now stored to the database i.e. $mock->uploadFile was called with parameter $newFile->uploadedFilename

I know that I can do ->with(...) when creating the mock object, but unfortunately this is not information I have beforehand. I first need to call the controller method which will need to instantiate an uploader instance.

I can duplicate the filename generation code in the test but that itself is going to be a problem if I update one part and forget to update the other.

So my question is, is there a way to assert that the mock method was called with specific parameters after the actual call was made?

I am open to any alternative approaches to this if there's something obvious I've missed with my approach.



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2FqymXm
via IFTTT

Aucun commentaire:

Enregistrer un commentaire