【问题标题】:PHPspec assign value and test updatePHPspec 赋值和测试更新
【发布时间】:2015-05-17 17:43:01
【问题描述】:

我有以下课程

class Customer {}

它具有诸如 Id、Name、City 和 Country 之类的属性以及诸如 findById 和 findByCity 之类的方法。我想编写一个规范测试来测试我的 Customer::save() 函数是否像这样工作

function it_should_update_customer_name_on_save()
{
$customer = $this->findById(1);
$customer->Name = 'Compu-Global-Hyper-Mega-Net';
$customer->save();

$this->findById(1)->shouldReturn('Compu-Global-Hyper-Mega-Net');
}

但是 phpspec 不断向我抛出这个错误

! it should update name on save (111ms)
        error: Argument 1 passed to PHPSpec2\Wrapper\ArgumentsUnwrapper::unwrapAll() must be of the type array, string given,
        called in /Users/kristiannissen/Documents/php/phpecosrv/vendor/phpspec/phpspec2/src/PHPSpec2/Prophet/ObjectProphet.php
        on line 126 and defined in
        /Users/kristiannissen/Documents/php/phpecosrv/vendor/phpspec/phpspec2/src/PHPSpec2/Wrapper/ArgumentsUnwrapper.php line
        10

我应该如何进行这种测试?

【问题讨论】:

  • 如果不是大量的代码,你能给我们足够的代码让我们自己重现错误吗? :)
  • $customer = $this->findById(1);返回 Customer() 的新实例。但是你将如何在 phpspec 中进行这种测试呢? 1) 找到一个现有的对象,2) 改变它的值,3) 保存它,4) 测试改变已经发生
  • @kristiannissen 我建议你先去阅读 phpspec 手册。您似乎缺少相当基本的概念:phpspec.net/en/latest/manual/introduction.html 另请阅读:techportal.inviqa.com/2013/07/23/…

标签: php oop phpspec


【解决方案1】:

您正在执行模型和数据库之间的集成测试,这不是 phpspec 的目标。 无论如何,客户的保存责任不应该在客户类中,它应该在域服务中。 findById 应由存储库类执行,save 应由实体管理器类执行,注入两者并模拟它们。

【讨论】:

    猜你喜欢
    • 2015-08-25
    • 2015-02-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-14
    相关资源
    最近更新 更多