2014-12-06 57 views
0

我RoR中新,我跟着一些教程就像Rails作爲殭屍,我必須在集成測試功能的問題:無法訪問內容JSON解析

test 'returns user by id' do 
    get "/users", {id: @user.id}, { :authorization => token_header(@user.authentication_token)} 
    assert_equal 200, response.status 
    user_response = JSON.parse(response.body, symbolize_names: true) 
    Rails.logger.debug "TEST: #{user_response}" 
    assert_equal @user.email, user_response[:email] 
end 

而且我得到這個錯誤:

ListingUsersTest#test_returns_user_by_id: 
    TypeError: no implicit conversion of Symbol into Integer 
    test/integration/listing_users_test.rb:23:in `[]' 
    test/integration/listing_users_test.rb:23:in `block in <class:ListingUsersTest>' 

我以爲USER_RESPONSE是空的或零或類似的東西,但在我的日誌:

TEST: [{:id=>428186149, :email=>"[email protected]",...}] 

所以我只是想知道如果我沒有錯過哈希結構的東西或..我不知道。

回答

1

你在你的JSON有數組

user_response[0][:email] 
# => "[email protected]" 

錯誤:

TypeError: no implicit conversion of Symbol into Integer 

表明[]預期的Integer,但得到了Symbol。這是一個暗示,儘管你期望在那裏有一個散列,但你有可能是一個數組。