Frank
Frank

Reputation: 189

How can i test my json in rails

I want to test my method Account.find whether it returns me the right result or not. I get the below error.

  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: undefined local variable or method response.

How can i check for the right response and response code.

Upvotes: 0

Views: 43

Answers (1)

Jorge de los Santos
Jorge de los Santos

Reputation: 4643

This won't work as what you want to actually test is the request answer. Try:

  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

Upvotes: 1

Related Questions