【问题标题】:PhpSpec - check method returns an integer rather than "willReturn(x)"PhpSpec - 检查方法返回一个整数而不是“willReturn(x)”
【发布时间】:2016-02-02 18:06:46
【问题描述】:

有没有一种方法可以测试getNumberOfProductsForCategory 方法将返回一个整数而不是特定 值(在这种情况下为18)?

我觉得简单地测试“18”虽然现在是正确的,但让我的测试变得非常脆弱,因为该值可能会在未来的某个时候发生变化,这意味着测试将失败。

这是我的 PhpSpec 函数(为简洁起见减少了):

    function it_returns_something_if_products_exist(
       ProductRepository $productRepository, 
       Category $category
    )
    {
       $category->getId()->willReturn(268);
       $productRepository->getNumberOfProductsForCategory($category)->willReturn(18);

       // call method here
    }

这是我的存储库方法:

/**
 * @param Category $category
 *
 * @return mixed
 */
public function getNumberOfProductsForCategory(Category $category)
{
    $dql = 'SELECT COUNT(tvp)
                FROM \CRMPicco\ProductBundle\Entity\Product tvp
            WHERE tvp.category = :category';

    return $this->getEntityManager()
        ->createQuery($dql)
        ->setParameter('category', $category)
        ->getSingleScalarResult();
}

【问题讨论】:

    标签: php testing tdd phpspec


    【解决方案1】:

    您没有在发布的代码中测试任何内容。 $productRepository$categorystubs 并且您所做的调用会配置它们的行为。

    例如,

    $category->getId()->willReturn(268);
    

    配置对象$category(它模仿Category类型的对象的行为)以在调用其方法getId()时返回268

    没有这个配置,$category->getId() 返回NULL

    其实phpspec不是一个测试工具,它是一个规范工具。它不用于测试代码的行为,而是用于描述其行为。看看phpspec 的创建者对limitations of phpspec 的评价。特别检查限制#8:

    问题是,PhpSpec 不是为集成测试而设计的——事实上,它并不是为测试任何东西而设计的。它的设计目的是帮助您设计出精心设计的课程。

    it 来自函数名称it_returns_blah_blah_blah() 指的是您描述的类,该类是提供规范名称的类。例如,如果您发布的函数是 FormatterSpec 类的成员,则意味着它描述了 Formatter 类的行为。

    it_returns_something_if_products_exist() 方法的名称应该表明它所描述的功能。

    假设您要描述方法Formatter::formatNumberOfProducts()。它有两个参数(Category $categoryProductRepository $productRepository)并返回一个字符串,它是 1 productX products(将 X 替换为类别中的实际产品数量)。

    规范可能如下所示:

    class FormatterSpec
    {
        function it_formats_number_of_products_when_they_are_many(
           ProductRepository $productRepository, 
           Category $category
        )
        {
            // prepare the stubs
            $category->getId()->willReturn(268);
            $productRepository->getNumberOfProductsForCategory($category)
                ->willReturn(18);
    
            // describe the behaviour
            $this->formatNumberOfProducts($category, $productRepository)
                ->shouldReturnString();
            $this->formatNumberOfProducts($category, $productRepository)
                ->shouldBe('18 products');
        }
    
    
        function it_formats_number_of_products_when_there_is_only_one(
           ProductRepository $productRepository, 
           Category $category
        )
        {
            // prepare the stubs
            $category->getId()->willReturn(268);
            $productRepository->getNumberOfProductsForCategory($category)
                ->willReturn(1);
    
            // describe the behaviour
            $this->formatNumberOfProducts($category, $productRepository)
                ->shouldReturnString();
            $this->formatNumberOfProducts($category, $productRepository)
                ->shouldBe('1 product');
        }
    }
    

    您需要配置存根,因为您知道formatNumberOfProducts() 方法调用了它们的一些方法。您使用存根是因为您不在乎$productRepository 如何获取它的数据;您只关心它返回一个数字,并且所描述的方法必须以特定方式使用该数字。

    【讨论】:

      猜你喜欢
      • 2014-04-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-11
      • 1970-01-01
      • 2021-01-20
      • 2019-06-20
      • 2021-04-11
      相关资源
      最近更新 更多