yii2
テスト
サーチ…
テスト環境の設定
コードのインストール:
composer global status
composer global require "codeception/codeception=~2.0.0" "codeception/specify=*" "codeception/verify=*"
Fakerをインストールする:
cd /var/www/yii // Path to your application
composer require --dev yiisoft/yii2-faker:*
テスト用にのみ使用されるデータベースを作成します。既存のデータベースを複製したり、移行を適用することができます。
cd tests
codeception/bin/yii migrate
tests/codeception/config/config-local.php
のcomponents['db']
設定を調整してtests/codeception/config/config-local.php
。
/var/www/yii/vendor/bin
ディレクトリをパスに追加します。
すべての設定ファイルと.yml
ファイルを確認してください。
ウェブサーバーを起動します。例:
php -S localhost:8080
テストを実行します。
codecept run
詳しくは:
- http://www.yiiframework.com/doc-2.0/guide-test-environment-setup.html
- http://codeception.com/install
- https://github.com/yiisoft/yii2-app-basic/tree/master/tests
- https://github.com/yiisoft/yii2-app-advanced/tree/master/tests
注:これらの手順は、Yii2バージョン2.0.9で有効です。バージョン2.0.10では、Sam Darkのテスト部分の再因子化された(そして命令を更新する必要があります)。バージョン2.0.10は、2016年9月11日にリリースされるはずです。https : //github.com/yiisoft/yii2/milestones
ActiveRecordをモックする方法
データベースに接続しようとしないARをモックしたい場合は、次のようにします(PHPUnitを使用している場合):
$post = $this->getMockBuilder('\app\model\Post')
->setMethods(['save', 'attributes'])
->getMock();
$post->method('save')->willReturn(true);
$post->method('attributes')->willReturn([
'id',
'status',
'title',
'description',
'text'
]);
このキャッチは、デフォルトでActiveRecordが回避しようとしているデータベーススキーマから属性リストを取得しているので、attributes()メソッドをオーバーライドする必要があることです。
Modified text is an extract of the original Stack Overflow Documentation
ライセンスを受けた CC BY-SA 3.0
所属していない Stack Overflow