简体   繁体   English

为什么我使用json_spec的Cucumber方案失败,并且last_json在哪里定义?

[英]Why does my Cucumber scenario that uses json_spec fail, and where is last_json defined?


It is a very newbie question: I am developing a REST API using Rails and I'd like to use json_spec to handle JSON in RSpec and Cucumber. 这是一个非常新手的问题:我正在使用Rails开发REST API,我想使用json_spec在RSpec和Cucumber中处理JSON。 I created my feature test (from 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"
    }
    """

But I got this error: 但是我得到了这个错误:

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

I think methods get and post are provided by capybara , but I cannot make the system recognize them. 我认为capybara提供了getpost方法,但是我无法使系统识别它们。

I also read that I'll need to define a last_json method but I don't know where I should add it. 我还读到我需要定义一个last_json方法,但是我不知道应该在哪里添加它。

Thanks! 谢谢!

As the blog post you cited says, you need to write the "I post" and "I get" steps. 您引用的博客文章所述 ,您需要编写“我发布”和“我得到”步骤。

The get and post methods available in Cucumber steps are provided by rack-test, which Capybara depends on. 黄瓜测试步骤中可用的getpost方法由机架测试提供,而Capybara则依赖该方法。 Capybara is not designed to POST programmatically , so I would write those steps using the rack-test methods: Capybara并非旨在以编程方式进行POST ,因此我将使用rack-test方法编写以下步骤:

When /^I get "(.*?)"$/ do |path|
  get path
end

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

Define last_json in env.rb, or in another .rb file in features/support. 在env.rb或功能/支持中的另一个.rb文件中定义last_json If you use rack-test, last_json will need to use rack-test too: 如果您使用rack-test, last_json也将需要使用rack-test:

def last_json
  last_response.body`enter code here`
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM