无法访问内容JSON解析
问题描述:
我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]",...}]
所以我只是想知道如果我没有错过哈希结构的东西或..我不知道。
答
你在你的JSON有数组:
user_response[0][:email]
# => "[email protected]"
错误:
TypeError: no implicit conversion of Symbol into Integer
表明[]
预期的Integer
,但得到了Symbol
。这是一个暗示,尽管你期望在那里有一个散列,但你有可能是一个数组。