源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

Yii2单元测试用法示例

  • 时间:2022-01-04 22:11 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:Yii2单元测试用法示例
本文实例讲述了Yii2单元测试用法。分享给大家供大家参考,具体如下: 使用composer方式安装yii2-app-basic (https://github.com/yiisoft/yii2-app-basic/blob/master/README.md) 装好后既可以使用 建一个Model文件EntryForm.php在models目录下
<?php
namespace app\models;
use Yii;
use yii\base\Model;
class EntryForm extends Model
{
  public $name;
  public $email;
  public function rules()
  {
    return [
      [['name', 'email'], 'required'],
      ['email', 'email'],
    ];
  }
}

建一个EntryFormTest.php放在tests/unit/models目录下
<?php
namespace tests\models;
use app\models\EntryForm;
class EntryFormTest extends \Codeception\Test\Unit
{
  public function testValidInput()
  {
    $model = new EntryForm();
    $model->name = 'Harry Qin';
    $model->email = '15848778@qq.com';
    expect_that($model->validate());
    return $model;
  }
  public function testInvalidInput()
  {
    $model = new EntryForm();
    $model->name = 'Harry Qin';
    $model->email = 'xxyy';
    expect_not($model->validate());
    $model = new EntryForm();
    $model->name = '';
    $model->email = '15848778@qq.com';
    expect_not($model->validate());
  }
  /**
   * 下面一行表示这里输入的参数值来自testValidInput的输出
   * @depends testValidInput
   */
  public function testModelProperty($model)
  {
    expect($model->name)->equals('Harry Qin');
  }
}

项目根目录下运行
composer exec codecept run unit

输出
。。。。。。
✔ EntryFormTest: Valid input (0.00s)
✔ EntryFormTest: Invalid input (0.00s)
✔ EntryFormTest: Model property (0.00s)

这里全部成功了,如果测试失败,会显示具体失败信息。 这里主要是3个方法 expect_that: 假设为true expect_not: 假设为false expect: 假设目标对象,后面可以接verify方法,具体方法列表在vendor/codeception/verify/src/Codeception/Verify.php文件中 更多关于Yii相关内容感兴趣的读者可查看本站专题:《[url=http://www.1sucai.cn/Special/386.htm]Yii框架入门及常用技巧总结[/url]》、《[url=http://www.1sucai.cn/Special/155.htm]php优秀开发框架总结[/url]》、《[url=http://www.1sucai.cn/Special/26.htm]smarty模板入门基础教程[/url]》、《[url=http://www.1sucai.cn/Special/43.htm]php面向对象程序设计入门教程[/url]》、《[url=http://www.1sucai.cn/Special/47.htm]php字符串(string)用法总结[/url]》、《[url=http://www.1sucai.cn/Special/84.htm]php+mysql数据库操作入门教程[/url]》及《[url=http://www.1sucai.cn/Special/231.htm]php常见数据库操作技巧汇总[/url]》 希望本文所述对大家基于Yii框架的PHP程序设计有所帮助。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部