我是黄瓜测试的新手。我正在尝试测试一个简单的 api,根据请求获取“/main/state_list”将返回 json 中的状态列表。
我的 .feature 文件
Feature: Check if valid states are returned in json
In order to select a state
As a user
I want to know if valid states are returned in json
Scenario: Get list of states
Given A GET request is made
Then the result should be list of states in JSON:
"""
[{"state":"California","_id":"4fbca64718a7ad0a68000003"},
{"state":"Georgia","_id":"4fbca69618a7ad0a68000005"}]
"""
我的 .rb 步骤文件
Given /^A GET request is made$/ do
get "/main/state_list"
end
Then /^the result should be list of states in JSON:$/ do |str|
json_data=JSON.parse(last_response.body)
json_data.should == JSON.parse(str)
end
我的控制器代码
def state_list
@states= State.all
render :json => @states.to_json
end
当我直接在浏览器中访问 url 时,我得到了正确的输出
当我在我的 cmd 中运行 cucumber 时,我得到了这个错误。基本上发送了空响应。
host is not a valid option for Mongo::Connection
Feature: Check if valid states are returned in json
In order to select a state
As a user
I want to know if valid states are returned in json
Scenario: Get list of states # features\check_list_of_states.feature:6
Given A GET request is made #features/step_definitionscheck_list_of_states_steps.rb:2
Then the result should be list of states in JSON: # features/step_definition/check_list_of_states_steps.rb:5
"""
[{"state":"California","_id":"4fbca64718a7ad0a68000003"},{"state":"Georgia","_id":"4fbca69618a7ad0a68000005"}]
"""
expected: [{"_id"=>"4fbca64718a7ad0a68000003", "state"=>"California"}, {"_
id"=>"4fbca69618a7ad0a68000005", "state"=>"Georgia"}]
got: [] (using ==)
Diff:
@@ -1,3 +1,2 @@
-[{"_id"=>"4fbca64718a7ad0a68000003", "state"=>"California"},
- {"_id"=>"4fbca69618a7ad0a68000005", "state"=>"Georgia"}]
+[]
(RSpec::Expectations::ExpectationNotMetError)
./features/step_definitions/check_list_of_states_steps.rb:8:in `/^the resu
lt should be list of states in JSON:$/'
features\check_list_of_states.feature:8:in `Then the result should be list
of states in JSON:'