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 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器