【问题标题】:How to get the message of exception in Behat test如何在 Behat 测试中获取异常消息
【发布时间】:2014-06-11 10:31:45
【问题描述】:

我是 behat 3 的新手,我将它与 symfony2 扩展一起使用,一切正常,但我需要在测试期间知道抛出异常的消息。假设我想开发一个简单的博客,所以我需要列出所有帖子,然后我写了一个功能和一个以下场景:

Feature: Browse posts
In order to browse all posts on the site
As a Visitor on the site
I need a site which shows list of all posts

Scenario: Listing all posts
Given I am on "/post"
And The database is clean
And There are following posts:
| title                       | content                     |
| Testowy tytuł posta         | Testowa treść posta         |
| Testowy tytuł posta numer 2 | Testowa treść posta numer 2 |
Then the response status code should be 200
Then I should see "All posts" in the "title" element
And I should see "Listing all posts"
And I should see "Testowy tytuł posta"

我实现了所需的 sn-ps 并且所有测试都通过了。好的,这次我需要在 url 中查看带有 slug 的博客文章,因此我将以下场景附加到上述功能:

Scenario: View one post by slug
Given I am on "/post/testowy-tytul-posta"
Then the response status code should be 200
Then I should see "Testowy tytuł posta" in the "title" element
And I should see "Testowa treść posta"

此时我在实体中没有 slug 字段,测试不应该通过并且它可以工作,但是控制台的输出如下:

Scenario: View one post by slug                                  # src/MyVendor/BlogBundle/Features/blog/listing_posts.feature:18
Given I am on "/post/testowy-tytul-posta"                      # MyVendor\BlogBundle\Features\Context\FeatureContext::visit()
Then the response status code should be 200                    # MyVendor\BlogBundle\Features\Context\FeatureContext::assertResponseStatus()
  Current response status code is 500, but 200 expected. (Behat\Mink\Exception\ExpectationException)
Then I should see "Testowy tytuł posta" in the "title" element # MyVendor\BlogBundle\Features\Context\FeatureContext::assertElementContainsText()
And I should see "Testowa treść posta"

行:

Current response status code is 500, but 200 expected

没有告诉我出了什么问题,当我使用 -v 选项运行 behat 命令(这会增加详细程度)时,它会给出以下输出:

Scenario: View one post by slug                                  # src/MyVendor/BlogBundle/Features/blog/listing_posts.feature:18
Given I am on "/post/testowy-tytul-posta"                      # MyVendor\BlogBundle\Features\Context\FeatureContext::visit()
Then the response status code should be 200                    # MyVendor\BlogBundle\Features\Context\FeatureContext::assertResponseStatus()
  Current response status code is 500, but 200 expected.

  +--[ HTTP/1.1 500 | http://localhost/post/testowy-tytul-posta | KernelDriver ]
  |
  |  <body>
  |          <div id="content">
  |              <div class="header clear-fix">
  |                  <div class="header-logo">
  |                      <img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAALYAAAA+CAMAAACxzRGDAAAAUVBMVEX////Ly8yko6WLioxkYmVXVVkwLjLl5eWxsLJKSEzy8vJxcHLY2Ni+vb89Oz9XVVh+fH+Yl5n///+xsbLY2Nlxb3KkpKWXlph+fX+LiYy+vr/IZP61AAAAAXRSTlMAQObYZgAABRBJREFUeNrVmtuWoyAQRS1FEEQSzQU7//+hYxUiXsKQZLJWM+chsUloN+WhCuguYoKyYqzmvGasKqH4HyRKxndipcgcumH8qViTM7TkUclcwaHmf5XM0eWq4km1KjdqXfMXJHVe1J3hL8lk5fCGv6wmT+o0d87U+XNrk0Y9nfv+7LM6ZJH5ZBL6LAbSxQ3Q5FDr22Skr8PQSy4n7isnsQxSX4r6pobhjCHHeDNOKrO3yGmCvZOjV9jmt8ulTdXFKdbKLNh+kOMvBzuVRa4Y7MUsdEUSWQe7xxCfZmcwjHU83LqzFvSbJQOXQvptbPnEFoyZtUUGwTeKuLuTHyT1kaP0P6cR01OKvv448gtl61dqZfmJezQmU/t+1R2fJLtBwXV6uWGwB9SZPrn0fKO2WAvQN1PUhHjTom3xgXYTkvlSKHs19OhslETq6X3HrXbjt8XbGj9b4Gi+lUAnL6XxQj8Pyk9N4Bt1xUrsLVN/3isYMug8rODMdbgOvoHs8uAb2fcANIAzkKCLYy+AXRpSU8sr1r4P67xhLgPp7vM32zlqt7Bhq2fI1Hwp+VgANxok59SsGV3oqdUL0YVDMRY7Yg8QLbVUU4NZNoOq5hJHuxEM28Sh/IyUZ8D3reR+yc...
  |
Then I should see "Testowy tytuł posta" in the "title" element # MyVendor\BlogBundle\Features\Context\FeatureContext::assertElementContainsText()
And I should see "Testowa treść posta"                         # MyVendor\BlogB

看起来有限,并没有显示所有响应内容。如果有一个选项只接受异常消息,那将非常有帮助 - 在这种情况下:

Entity 'MyVendor\BlogBundle\Entity\Post' has no field 'slug'. You can therefore not call 'findOneBySlug' on the entities' repository

并将其提供给输出而不是响应。

【问题讨论】:

    标签: symfony behat


    【解决方案1】:

    我需要做的就是在 Behat\MinkExtensio 下添加以下行到 behat.yml:

    [...]
    Behat\MinkExtension:
        show_auto: true
        show_cmd: firefox %s
    [...]
    

    这将停止对失败步骤的测试并使用给定的结果打开浏览器。

    【讨论】:

      猜你喜欢
      • 2016-04-30
      • 1970-01-01
      • 2011-06-08
      • 2011-08-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-14
      • 2017-03-25
      相关资源
      最近更新 更多