如何断言文本仅在Mink中存在一次

时间:2013-10-03 14:05:16

标签: behat mink

我目前正在开展一个小项目,我决定玩一下Behat / Mink,我遇到了第一个问题,我无法单独解决。

我有这个功能,它按预期工作

Scenario: Create Customer
  Given I am on "/login"
  When  I fill in "username" with "testuser"
  And   I fill in "password" with "123"
  And   I press "Login"
  And   I follow "Customers"
  And   I follow "Create"
  Then  I should be on "/customer/new"
  And   I fill in "email" with "test@test.de"
  And   I press "Save"
  Then  I should be on "/customer/"
  And   I should see "test@test.de"

当我按下“保存”项目时,检查电子邮件是否已存在。如果是这样,它只是重定向到/ customer。我如何断言,我的页面上只有一次文本(不是两次或更多次)?

1 个答案:

答案 0 :(得分:3)

您可以写一个新步骤,例如:

/**
  * @Then /^I should see "([^"]*)" exactly "([^"]*)" times$/
  */
public function iShouldSeeTextSoManyTimes($sText, $iExpected)
{
    $sContent = $this->getSession()->getPage()->getText();
    $iFound = substr_count($sContent, $sText);
    if ($iExpected != $iFound) {
        throw new \Exception('Found '.$iFound.' occurences of "'.$sText.'" when expecting '.$iExpected);
    }
}

然后有一个场景,如:

Scenario: Some many times
Given I am on "http://en.wikipedia.org"
Then I should see "Welcome" exactly "1" times