duanhemou9834 2013-09-23 14:44
浏览 75
已采纳

Yii单元测试,yii中的常量

I'm writing unit tests in Yii, and I need a way to understand I'm in test mode(for default scopes and ..).I've defined a constant in index-test.php. like:

defined('YII_TEST_MODE') or define('YII_TEST_MODE',true);

in my model:

if(YII_TEST_MODE){ ...

but in my code when I'm checking this it show this error:

Use of undefined constant YII_TEST_MODE - assumed 'YII_TEST_MODE'

Is there a better way to know tell yii you are in test mode? and also am I using wrong syntax?

  • 写回答

2条回答 默认 最新

  • dongxibo2095 2013-09-24 06:16
    关注

    you want to be very careful about using if(TEST) in your code, the purpose of unit tests is to test the actual code that will be used for a specific function/class.

    Doing it the way you are, only ensures that the code works in test mode, not in real mode - which defies the point.

    If you follow the paradigm that each functional class should have its own set of unit tests, then mock the other classes that it interacts with, rather than doing specific actions when in test mode.

    For example, if you are testing the functionality of your model class, and you dont want to save data to a database, you can instead mock the database connection with one that does not actually store any data.

    That being said, if you note your config folder, you will likely see that you have a main.php and a test.php. What you can do is define a variable "is_test" as follows:

    'params'=>array(
      'is_test'=>false
    )
    

    you put that in your main.php, and in your test.php you set it to true. Then you can check Yii::app()->params["is_test"]

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器