UnderTaker
UnderTaker

Reputation: 893

Checking whether the json response is right or not

I get a json response from my server.

{
    "@type": "res",
    "createdAt": "2014-07-24T15:26:49",
    "description": "test",
    "disabled": false
}

How can i test whether the response is right or wrong. Below is my test case.

it "can find an account that this user belongs to" do 
    Account.find(id: @acc.id, authorization: @token);

    expect(response.status).to eq 200
    expect(response.body).to eq({
      "@type": "res",
      "createdAt": "2014-07-24T15:26:49",
      "description": "test",
      "disabled": false
    })
end

When i try to execute the test, it throws me a lot of syntax error.

Upvotes: 1

Views: 165

Answers (1)

Roman Kiselenko
Roman Kiselenko

Reputation: 44380

You should parse JSON.parse(response.body), because body as string:

it "can find an account that this user belongs to" do 
    Account.find(id: @acc.id, authorization: @token)
    valid_hash = {
      "@type" => "res",
      "createdAt" => "2014-07-24T15:26:49",
      "description" => "test",
      "disabled" => false
    }
    expect(response.status).to eq 200
    expect(JSON.parse(response.body)).to eq(valid_hash)
end

Or without parsing:

it "can find an account that this user belongs to" do 
    Account.find(id: @acc.id, authorization: @token)
    valid_hash = {
      "@type" => "res",
      "createdAt" => "2014-07-24T15:26:49",
      "description" => "test",
      "disabled" => false
    }
    expect(response.status).to eq 200
    expect(response.body).to eq(valid_hash.to_json)
end

Update, you hash syntax invalid instead:

 valid_hash = {
      "@type": "res",
      "createdAt": "2014-07-24T15:26:49",
      "description": "test",
      "disabled": false
    }

use:

 valid_hash = {
      "@type" => "res",
      "createdAt" => "2014-07-24T15:26:49",
      "description" => "test",
      "disabled" => false
    }

Upvotes: 1

Related Questions