2012-05-24 54 views
0

我是黄瓜测试的新手。我试图测试一个简单的api,根据请求获取“/ main/state_list”将返回json中的状态列表。使用在轨道上使用红宝石搭建的黄瓜测试api

我.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中运行黄瓜时,出现此错误。基本上空的响应被发送。

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:' 

回答

0

我怀疑你的测试数据库没有填充与开发数据库相同的数据。

+0

感谢您的回复,但发现它回来:) –