这是一个非常新手的问题:我正在使用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
我认为方法获取和发布由capybara提供,但我无法让系统识别它们。
我还读到我需要定义 last_json 方法,但我不知道应该在哪里添加它。
谢谢!
答案 0 :(得分:1)
正如the blog post you cited所说,你需要写一个"我发布"和#34;我得到"步骤。
Cucumber步骤中可用的get
和post
方法由机架测试提供,Capybara依赖于此。 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中定义last_json
,或在features / support中定义另一个.rb文件。如果您使用机架测试,last_json
也需要使用机架测试:
def last_json
last_response.body`enter code here`
end