在功能测试中模拟 class 实例化

Mocking class instantiation in a feature test

我目前正在处理我的第一个 Laravel 项目 — 一个服务端点,returns 一个基于保存在 S3 中的记录的资源。该服务不需要数据库,但我的想法是,我可以通过将逻辑移动到 "model" 来保持控制器的瘦身。然后我可以通过模仿一些标准的活动记录调用来访问资源。

在功能上,实现按预期工作,但我在模拟方面遇到问题。

我正在使用一个库来创建签名的 CloudFront URL,但它是作为静态方法访问的。当我第一次开始编写我的特性测试时,我发现我无法对静态方法进行存根。我尝试了 class 与 Mockery 的别名,但没有成功——我仍然在使用静态方法。因此,我尝试将静态方法包装在一点 class 中,假设模拟 class 会更容易。不幸的是,我遇到了同样的问题。我试图嘲笑的东西被击中了,就好像我没有嘲笑它一样。

这个堆栈溢出 post 给出了一个如何使用 class 别名的示例,但我无法让它工作。

我做错了什么?我宁愿让嘲笑别名工作,但实例嘲笑会很好。请指出正确的方向。预先感谢您的帮助。

控制器

// app/Http/Controllers/API/V1/RecordingController.php
class RecordingController extends Controller {
    public function show($id){
        return json_encode(Recording::findOrFail($id));
    }
}

型号

// app/Models/Recording.php
namespace App\Models;

use Mockery;
use Carbon\Carbon;
use CloudFrontUrlSigner;
use Storage;
use Illuminate\Support\Arr;

class Recording
{
    public $id;
    public $url;

    private function __construct($array)
    {
        $this->id = $array['id'];
        $this->url = $this->signedURL($array['filename']);
    }

    // imitates the behavior of the findOrFail function
    public static function findOrFail($id): Recording
    {
        $filename = self::filenameFromId($id);
        if (!Storage::disk('s3')->exists($filename)) {
            abort(404, "Recording not found with id $id");
        }

        $array = [
            'id' => $id,
            'filename' => $filename,
        ];

        return new self($array);
    }

    // imitate the behavior of the find function
    public static function find($id): ?Recording
    {
        $filename = self::filenameFromId($id);
        if (!Storage::disk('s3')->exists($filename)){
            return null;
        }

        $array = [
            'id' => $id,
            'filename' => $filename,
        ];

        return new self($array);
    }

    protected function signedURL($key) : string
    {
        $url = Storage::url($key);
        $signedUrl = new cloudFrontSignedURL($url);
        return $signedUrl->getUrl($url);
    }
}

/**
 * wrapper for static method for testing purposes
 */
class cloudFrontSignedURL {
    protected $url;
    public function __construct($url) {
        $this->url = CloudFrontUrlSigner::sign($url);
    }
    public function getUrl($url) {
        return $this->url;
    }
}

测试

// tests/Feature/RecordingsTest.php
namespace Tests\Feature;

use Mockery;
use Faker;
use Tests\TestCase;
use Illuminate\Http\File;
use Illuminate\Support\Facades\Storage;
use Illuminate\Foundation\Testing\WithFaker;

/* The following is what my test looked like when I wrapped CloudFrontUrlSigner 
 * in a class and attempted to mock the class
 */
class RecordingsTest extends TestCase
{
    /** @test */
    public function if_a_recording_exists_with_provided_id_it_will_return_a_URL()
    {
        $recordingMock = \Mockery::mock(Recording::class);
        $faker = Faker\Factory::create();
        $id = $faker->numberBetween($min = 1000, $max = 9999);

        $filename = "$id.mp3";
        $path = '/api/v1/recordings/';
        $returnValue = 'abc.1234.com';

        $urlMock
            ->shouldReceive('getURL')
            ->once()
            ->andReturn($returnValue);

        $this->app->instance(Recording::class, $urlMock);

        Storage::fake('s3');
        Storage::disk('s3')->put($filename, 'this is an mp3');
        Storage::disk('s3')->exists($filename);

        $response = $this->call('GET', "$path$id");
        $response->assertStatus(200);
    }
}

// The following is what my test looked like when I was trying to alias CloudFrontUrlSigner
{
    /** @test */
    public function if_a_recording_exists_with_provided_id_it_will_return_a_URL1()
    {
        $urlMock = \Mockery::mock('alias:Dreamonkey\cloudFrontSignedURL');
        $faker = Faker\Factory::create();
        $id = $faker->numberBetween($min = 1000, $max = 9999);

        $filename = "$id.mp3";
        $path = '/api/v1/recordings/';
        $returnValue = 'abc.1234.com';

        $urlMock
            ->shouldReceive('sign')
            ->once()
            ->andReturn($returnValue);

        $this->app->instance('Dreamonkey\cloudFrontSignedURL', $urlMock);

        Storage::fake('s3');
        Storage::disk('s3')->put($filename, 'this is an mp3');
        Storage::disk('s3')->exists($filename);

        $response = $this->call('GET', "$path$id");
        $response->assertStatus(200);
    }
}

phpunit

$ phpunit tests/Feature/RecordingsTest.php --verbose

...

There was 1 failure:

1) Tests\Feature\RecordingsTest::if_a_recording_exists_with_provided_id_it_will_return_a_URL
Expected status code 200 but received 500.
Failed asserting that false is true.

/Users/stevereilly/Projects/media-service/vendor/laravel/framework/src/Illuminate/Foundation/Testing/TestResponse.php:133
/Users/stevereilly/Projects/media-service/tests/Feature/RecordingsTest.php:85
/Users/stevereilly/.composer/vendor/phpunit/phpunit/src/TextUI/Command.php:206
/Users/stevereilly/.composer/vendor/phpunit/phpunit/src/TextUI/Command.php:162

您得到了 500,这意味着代码有问题。只是通过扫描它,我注意到你在 Recordings class 上缺少一个 filenameFromId 方法,并且测试正在创建一个名为 $recordingMock 的模拟,但你尝试使用 $urlMock。先尝试解决这些问题。
然后你在嘲笑 class,但你从来没有在你的应用程序中替换它(你显然在旧测试中做到了)。
通常你想在模拟时遵循这些步骤:
1.模拟一个class
2. 告诉 Laravel 在有人请求时用你的 mock 替换 class
3. 对 mock

做一些断言