douzhuolong9886 2014-11-17 22:24
浏览 19
已采纳

Phpspec测试类构造函数

I've just started using Phpspec on my Laravel project.

The class I'm testing has the following schema:

class Card
{
    /**
     * @var int
     */
    private $rank;

    /**
     * @var int
     */
    private $suit;

    /**
    * @param int $rank
    * @param int $suit
    */
    public function __construct($rank = null, $suit = null);

    /**
     * @return int
     */
    public function getRank();

    /**
     * @return int
     */
    public function getSuit();

    /**
     * @param int $rank
     */
    private function setRank($rank);

    /**
     * @param int $suit
     */
    private function setSuit($suit);
}

I didn't include the actual code as it is quite simplistic. It throws exceptions in case parameters are invalid, but let me know if you'd like me to update this with code. And here's the spec class:

class CardSpec extends ObjectBehavior
{
    const INVALID_PARAM = 'WontWork';
    const INVALID_RANK = 14;
    const INVALID_SUIT = 5;

    const VALID_RANK = 12;
    const VALID_SUIT = 2;

    function it_is_initializable()
    {
        $this->shouldHaveType('Shazzam\Games\Blackjack\Card');
    }

    function it_throws_exception_on_invalid_arguments()
    {
        $this->shouldThrow('\InvalidArgumentException')->during('__construct', [self::INVALID_PARAM, 0]);
        $this->shouldThrow('\InvalidArgumentException')->during('__construct', [self::INVALID_RANK, self::VALID_SUIT]);
        $this->shouldThrow('\InvalidArgumentException')->during('__construct', [self::VALID_RANK, self::INVALID_SUIT]);
    }

    function it_creates_an_object_successfully()
    {
        // Would like to pass parameters and check its values using the get methods
    }
}

I've been able to check it is throwing an exception for invalid arguments, but now I want to make sure it is passing the construct validation and returning the values when valid.

I've read the documentation and couldn't find a way to achieve this, which leads me to believe that I'm not fully understanding BDD.

Is this kind of test a unit test and should therefore be separate from behavior tests?

  • 写回答

1条回答 默认 最新

  • duanqian1888 2014-11-18 01:00
    关注

    After some more reading I've come across this nice post:

    https://laracasts.com/discuss/channels/general-discussion/codeception-phpspec-acceptance-functional-integration-unit-testing

    Check austenc and ozanhazer answers if you are having the same doubts.

    As in that post, phpspec isn't meant for functional and acceptance tests.

    Check that post or google to match the right tools with the kinds of tests you want to implement.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划