我如何在rails中测试我的json

时间:2014-08-07 09:36:06

标签: ruby-on-rails ruby json ruby-on-rails-4

我想测试我的方法Account.find是否返回正确的结果。我得到以下错误。

  it "can find an account " do 
    Account.find(id: @acc.id, authorization: @token);         
    expect(response.status).to have_http_status(200);
    body = JSON.parse(response.body)
    expect(body["name"]).to eq "Test"    
  end
  

NameError:未定义的局部变量或方法响应。

如何检查右responseresponse code

1 个答案:

答案 0 :(得分:1)

这不起作用,因为你想要实际测试的是请求答案。尝试:

  it "can find an account that this user belongs to" do 
    get "/accounts/#{@acc.id}/", headers # like the token or whatever your api needs

    expect(response).to be_success 

    # now you have the response.body available to do:
    body = JSON.parse(response.body)
    expect(body["name"]).to eq "Test"  

  end