PyStringNode 和 json 字符串

PyStringNode and json string

我有以下功能,我正在尝试测试对象的结构。

  @mynet
  Scenario: Import mynet network Transactions
    Given I Import transactions for network "mynet" from "2015-09-01" to "2015-09-01"
    Then I should have 20 transactions imported
    And The first element of the transaction should be
    """
    {
            "transaction_date": "2015-09-01 19:34:17",
            "tenant_id":  1,
            "network":  "xxx",
            "network_transaction_id":  "xxxxa630514d",
            "network_merchant_name":  "xxxxx",
            "currency":  "EUR",
            "amount":  "287.40",
            "commission":  "4.31",
            "subid":  "xxxxxxxxxx",
            "enquiry_id":  0,
            "reference":  "xxxx6a630514d",
            "network_merchant_id":  "4",
            "status":  "declined",
            "network_status":  "2",
            "meta_data":  "{"short_name":"mynet"}"
    }
    """

meta_data 部分有问题。我感觉 PyStringNode 格式没有正确转换它。 这是我的 Behat 上下文中用于处理 PyStringNode

的部分
public function theFirstElementOfTheTransactionShouldBe(PyStringNode $string)
{
    $expectedFirstElement = json_decode($string->getRaw(), true);
    $realfirstelement = $this->transactions[0];
    Assert::assertEquals($realfirstelement, $expectedFirstElement);
}

当 运行 behat 测试时,我得到错误

null does not match expected type "array".

当我用一个简单的字符串替换 meta_data 时,一切正常.....有 json 对象时有特殊情况吗?如何构建我的 scenario/feature?

您的对象周围有多余的引号。删除它们,它应该可以工作。

这是更正后的版本(未测试):

@mynet
  Scenario: Import mynet network Transactions
    Given I Import transactions for network "mynet" from "2015-09-01" to "2015-09-01"
    Then I should have 20 transactions imported
    And The first element of the transaction should be
    """
    {
        "transaction_date": "2015-09-01 19:34:17",
        "tenant_id":  1,
        "network":  "xxx",
        "network_transaction_id":  "xxxxa630514d",
        "network_merchant_name":  "xxxxx",
        "currency":  "EUR",
        "amount":  "287.40",
        "commission":  "4.31",
        "subid":  "xxxxxxxxxx",
        "enquiry_id":  0,
        "reference":  "xxxx6a630514d",
        "network_merchant_id":  "4",
        "status":  "declined",
        "network_status":  "2",
        "meta_data": {
            "short_name":"mynet"
        }
    }
    """