【问题标题】:codeception Example to Array数组的代码接收示例
【发布时间】:2017-10-23 15:14:25
【问题描述】:

是否有实现方法将示例转换为数组,以便我可以将其作为参数传递给发布请求。现在我必须做:

/**
     * @dataprovider invalidUserActivityRequestProvider
     */
    public function it_does_not_get_tracked_for_an_invalid_request(ApiTester $I, Example $example)
    {
        $userActivity = [
            'timestamp' => isset($example['timestamp']) ? $example['timestamp'] : null ,
            'email' => isset($example['email']) ? $example['email'] : null ,
            'type' => isset($example['type']) ? $example['type'] : null ,
            'duration' => isset($example['duration']) ? $example['duration'] : null ,
            'distance' => isset($example['distance']) ? $example['distance'] : null ,
            'repetitions' => isset($example['repetitions']) ? $example['repetitions'] : null ,
        ];

        $I->sendPOST('/useractivity', $userActivity);
        $I->seeResponseCodeIs(422);
    }

    protected function invalidUserActivityRequestProvider () : array {
        return [
            [
                'timestamp' => Carbon::now()->toDateTimeString(),
                'email' => 'invalidUser@example.com',
                'type' => 'run',
                'duration' => 300,
                'distance' => 1000,
                'repetitions' => 1
            ],

            [
                'timestamp' => Carbon::now()->toDateTimeString(),
                'email' => 'user@example.com',
                'type' => 'invalidType',
                'duration' => 300,
                'distance' => 1000,
                'repetitions' => 1
            ],

            [
                'timestamp' => Carbon::now()->toDateTimeString(),
                'email' => 'user@example.com',
                'type' => 'run'
            ],
        ];
    }

但我想要类似的东西:

public function it_does_not_get_tracked_for_an_invalid_request(ApiTester $I, Example $example)
    {
        $I->sendPOST('/useractivity', $example->toArray());
        $I->seeResponseCodeIs(422);
    }

【问题讨论】:

  • Example $example 表示 $example 是类 Example 的对象,但您将其用作数组 $example['timestamp'] 所以我不知道您在这里做什么。
  • 然而,$I->sendPOST('/useractivity', (array)$example);
  • @AbraCadaver Example 允许以这种方式访问​​属性。请看:codeception.com/docs/07-AdvancedUsage
  • @AbraCadaver :) 尽管如此,它确实有效
  • 是的,我不知道 codeception 所以也许其他人会有更好的主意。

标签: php testing codeception


【解决方案1】:

我不知道 Codeception,但可以选择强制转换:

$I->sendPOST('/useractivity', (array)$example);

【讨论】:

    【解决方案2】:

    对不起,它真的不起作用!...

    尝试 assertEqual 一个 (array) $example 和一个数组会导致:

    - Expected | + Actual
    @@ @@
    Array (
    -    Binary String: 0x002a0064617461 => Array (...)
    +    'id' => 6
    +    'unique_id' => '2199033651380'
    +    'data' => Array (...)
    +    'user_id' => 1
    +    'brand_id' => 1
    +    'infrastructure' => 'AT'
    )
    

    【讨论】:

      猜你喜欢
      • 2022-09-28
      • 2011-07-21
      • 2013-06-03
      • 2013-11-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-07
      • 1970-01-01
      • 2013-01-29
      相关资源
      最近更新 更多