【问题标题】:Cucumber and DELETE with MongoidCucumber 和 DELETE 与 Mongoid
【发布时间】:2011-08-18 19:56:59
【问题描述】:

所以,我目前正在尝试用 Cucumber 测试一个用 Mongoid 连接的 Rails 应用程序。我已经完成了所有设置(或者我相信),以便运行以下测试:

Feature: Create and manage about entries
  In order to use the about data effectively
  As an application consumer
  I want to create and manage some about entries

  Scenario: Create about entries
    Given the following abouts exist
      | title             |   body_copy   |
      | "About entry #1"  | "hello body!" |
    When I go to the list of about entries
    Then I should see "About entry #1"

  Scenario: Create and retreive specific about entry
    Given the following abouts exist
      | id                        | title             |   body_copy   |
      | 4e4d37756ea257f031000003  | "About entry #1"  | "hello body!" |
    When I go to about entry with id 4e4d37756ea257f031000003
    Then I should see "About entry #1"

在我的路径文件中,我有以下条目来支持上述测试:

when /^the list of about entries$/i
  '/abouts'
when /^about entry with id (.+)$/i
  "/abouts/#{$1}"

这些测试效果很好。但是,我需要测试删除操作。我在网上做了一些研究,但似乎一切都在通过 UI 删除这些项目,我遇到的问题是我的 Rails 应用程序只提供 JSON 文件和 JSON 文件,我需要一种更好(更编程)的方式来测试没有UI 参与其中。就模拟而言,我使用的是 Pickle 内置的默认模拟。如有必要,我愿意使用其他模拟软件,例如 factory-girl,但你必须给我一些详细的反馈,我该如何连接它。我目前的删除测试(不起作用)是:

  Scenario: Delete about
    Given the following abouts exist
      | title   | body_copy    |
      | title 1 | body_copy 1  |
      | title 2 | body_copy 2  |
      | title 3 | body_copy 3  |
      | title 4 | body_copy 4  |
    When I delete the 3rd about
    Then I should see the following abouts:
      | Title   | body_copy    |
      | title 1 | body_copy 1  |
      | title 2 | body_copy 2  |
      | title 4 | body_copy 4  |

问题在于自动生成的测试(如上所示)使用了click_link "Destroy" 方法调用,但这不起作用。

【问题讨论】:

    标签: ruby-on-rails ruby cucumber mongoid


    【解决方案1】:

    您需要更改删除步骤的实现(如果您使用 Cucumber 生成的默认值,则在您的 web_steps.rb 中)以发送 DELETE HTTP 请求。我会为此推荐 RestClient gem,但还有很多其他选择。

    PragProg 目前处于测试阶段的 Cucumber Book 有一章是关于使用 Cucumber 来测试这样的 REST API。

    【讨论】:

    • 发送 HTTP DELETE 请求需要我运行 rails 服务器。我对 Rails 测试不太熟悉,所以你是说当我运行测试并且 Rails 实例也在运行时已经是这种情况了吗?步骤定义的示例将非常有帮助。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-12
    • 1970-01-01
    • 1970-01-01
    • 2012-08-25
    • 1970-01-01
    相关资源
    最近更新 更多