LoginSignup
5

More than 5 years have passed since last update.

コントローラのユニットテストでHTMLを描画しようとするとAssetファイルが正常の読み込まれない

Posted at

例えば以下のようなテストを書いた場合。

hogehoge_test.php
public function test_index()
{
    $request = \Fuel\Core\Request::forge('/');
    $response = $request->execute()->response();

    // この行のrender()メソッドでエラーが出る。
    $html = $response->body()->render();
    $pattern = '/' . preg_quote('トップページ', '/') . '/u';
    $this->assertRegExp($pattern, $body);
}

以下のようなエラーが出る。

メッセージ
Fuel\Core\FuelException: Could not find asset: bootstrap.css

原因は、Assetファイルを探索する際の処理で、
カレントディレクトリがドキュメントルート(public/)になっていることが想定されているから。
これはデフォルトの挙動で、設定すれば変更することができる。

Assetクラスのオプションを設定するので、
以下のような内容のfuel/app/config/test/asset.phpというファイルを設置する。

<?php
return array(
    'paths' => [DOCROOT.'public/assets/']
);

これで無事にAssetファイルが読み込まれるようになった。

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5