BDD和Behat特殊字符

3

有一个类似的问题 @ Gherkin in Behat and input validations scenarios

但是并不完全相同。

我的问题是我需要将场景大纲示例添加到数组中。

Given I have a problem with data
   | in  | this    | array      |
   | how | can     | I          |
   | add | special | characters | 

大多数特殊字符都可以,但引号和管道符怎么办?

special characters example: \|!"#$%&/()=?«»'{}[]'`^~*+ºª-_.:,;<>@ł€¶ŧ←↓→øþĸħŋđðßæ|«»¢“”nµ

谢谢

4个回答

3

我知道这篇文章已经发布了一年,但今天我遇到了类似的问题,并在这里发布了我的解决方案。

为了防止Google Groups的帖子被删除,我在此处复制:

问题

我的.feature文件大致如下:

 Then I get a response with "error" equals to "<error>"

 And I get a response with "error" equals to "<error_message>"


 Examples:

 |error                    |  error_message                                                              |

 |NotFoundHttpException    |  Something with quotes "More text here"                     |

你可以看到,我在调用同一个步骤来检查文本中包含引号和不包含引号的另一列。

当我运行Behat测试时,"More text here"被视为另一个参数,并且Behat会建议提供另一个片段。

解决方案

为了解决这个问题,我们需要使用与"不同的其他字符来告诉Behat存在一个变量,在我的情况下,我使用了单引号。

因此,我已经像这样更改了.feature文件:

 Then I get a response with "error" equals to "<error>"

 And I get a response with "error_message" equals to '<error_message>' escaping quotes

 Examples:

 |error                    |  error_message                                                            |

 |NotFoundHttpException    |  Something with quotes "More text here"                     |

那么我已经更新了我的PHP测试实现,就像这样:

/**
 * @Then I get a response with :attibute equals to :value
 * @Then /^I get a response with "([^"]+)" equals to '([^']+)' escaping quotes$/
 */
public function iGetAResponseWithEqualsTo($attibute, $value)

这个实现被称为“非常相似的实现”。

我在阅读这篇文章后想到了这个解决方案,以备有需要的人参考。


3
我在使用Behat 3.0时发现,我只需使用''作为分隔符即可指定字符串,例如: Then I should see the text 'some "text" with "quotes"' 它可以直接使用,无需编写自定义步骤。请注意保留HTML标记。

确认使用Behat 3.5时,特别是需要使用json时:Then the response json should have fragment '{"username": "admin"}'非常好用。 - yaroslavche

2
我发现一个可行的替代方案是匹配两个步骤定义,一个用双引号括起来要匹配的字符串,另一个在步骤下面用三个引号括起来的PyString

例如:

And the email body should contain
"""
This link with double quotes <a href="http://mockshare.url/file.pdf">DOWNLOAD</a>
"""

方法:
/**
 * Checks that the body of the last accessed email contains a string.
 *
 * @param mixed $body The string or PyString representing text to match.
 *
 * @uses A PyString class sometimes, to help with double quotes in the matched string. 
 * (Hence the string casting).
 *
 * @Then the email body should contain :body
 * @Then the email body should contain
 *
 * @return void
 */
public function theEmailBodyShouldContain($body)
{
    assertContains((string) $body, $this->body);
}//end theEmailBodyShouldContain()

1
在尝试中,我发现如果始终使用三个引号,则行* @Then the email body should contain:body是不必要的。 - scones

0

发现问题需要使用转义反斜杠“\”

我尝试过这个,但是在我使用的编辑器上它没有被认为是转义字符,所以需要进行功能测试来验证,使用\"或\|可以按预期工作。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接