【问题标题】:Why does my Cucumber scenario that uses json_spec fail, and where is last_json defined?为什么我使用 json_spec 的 Cucumber 场景失败,last_json 定义在哪里?
【发布时间】:2014-06-24 12:39:23
【问题描述】:


这是一个非常新手的问题:我正在使用 Rails 开发一个 REST API,我想使用json_spec 来处理 RSpec 和 Cucumber 中的 JSON。 我创建了我的功能测试(来自here):

  Feature: User API
  Scenario: User list
  Given I post to "/users.json" with:
    """
    {
      "first_name": "Steve",
      "last_name": "Richert"
    }
    """
  And I keep the JSON response at "id" as "USER_ID"
  When I get "/users.json"
  Then the JSON response should have 1 user
  And the JSON response at "0" should be:
    """
    {
      "id": %{USER_ID},
      "first_name": "Steve",
      "last_name": "Richert"
    }
    """

但是我收到了这个错误:

Given(/^I post to "(.*?)" with:$/) do |arg1, string|
  pending # express the regexp above with the code you wish you had
end

When(/^I get "(.*?)"$/) do |arg1|
  pending # express the regexp above with the code you wish you had
end

我认为getpost方法是capybara提供的,但我无法让系统识别它们。

我还读到我需要定义一个 last_json 方法,但我不知道应该在哪里添加它。

谢谢!

【问题讨论】:

    标签: ruby-on-rails json ruby rspec cucumber


    【解决方案1】:

    正如the blog post you cited 所说,您需要编写“我发布”和“我得到”步骤。

    Cucumber 步骤中可用的 getpost 方法是由 Capybara 所依赖的 rack-test 提供的。 Capybara is not designed to POST programmatically,所以我会使用机架测试方法编写这些步骤:

    When /^I get "(.*?)"$/ do |path|
      get path
    end
    
    Given /^I post to "(.*?)" with:$/ do |path, body|
      post path, body
    end
    

    在 env.rb 或 features/support 中的另一个 .rb 文件中定义 last_json。如果你使用 rack-test,last_json 也需要使用 rack-test:

    def last_json
      last_response.body`enter code here`
    end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-23
      • 2018-07-25
      • 1970-01-01
      相关资源
      最近更新 更多