【问题标题】:How can i test my json in rails我如何在 Rails 中测试我的 json
【发布时间】:2014-08-07 09:36:05
【问题描述】:

我想测试我的方法 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

【问题讨论】:

  • 粘贴 Account 类
  • @jvnill:我已经更新了 Account 类。
  • 当您测试控制器时,您将获得响应。在模型测试中未定义。
  • @Saravanan:你能给我看一下吗?
  • account = Account.find(id: @acc.id, 授权: @token); account.name.should == "测试"

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


【解决方案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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-23
    • 2012-02-05
    • 2018-03-05
    • 1970-01-01
    • 1970-01-01
    • 2018-07-27
    相关资源
    最近更新 更多