UnderTaker
UnderTaker

Reputation: 893

Getting html instead of json respone

I am getting a html instead of json response when i test. Below is code.

require 'spec_helper'

describe AccController, :type => :controller do
  before(:all) do

    user = User.login(FactoryGirl.create(:user, email: "[email protected]", password: "test", code: 0));
    if user
      @auth = user['token']
    end
  end

  it "can find an account that this user belongs to" do 
    Account.should_receive(:find, with: {id: 2055, authorization: @auth})

    get :show, id:2055

    hashed_response = {
        "@type" => "test",
        "createdAt" => "2014-08-07T14:31:58.198",
        "createdBy" => 2,
        "updatedAt" => "2014-08-07T14:31:58.247",
        "updatedBy" => 2,
        "accountid" => 2055,
        "name" => "test",
        "description" => "Something about test",
        "disabled" => false
    }

    expect(response.status).to eq 200    
    expect(response.body).to eq(hashed_response.to_json);

  end

end

Below is what my rspec is failure message.

Failure/Error: expect(response.body).to eq(hashed_response.to_json);      
   expected: "{\"@type\":\"test\",\"createdAt\":\"2014-08-07T14:31:58.198\",\"createdBy\":2,\"updatedAt\":\"2014-08-07T14:31:58.247\",\"updatedBy\":2,\"accountid\":2055,\"name\":\"test\",\"description\":\"Something about test\",\"disabled\":false}"
        got: "<html><body>You are being <a href=\"http://test.host/login\">redirected</a>.</body></html>"

I want to check whether my find method yeilds the right response by comparing.

Upvotes: 0

Views: 153

Answers (2)

wicz
wicz

Reputation: 2313

Try to change the request to

xhr(:get, :show, id: "2055", format: "json")

Upvotes: 0

Linus Oleander
Linus Oleander

Reputation: 18127

You need to set what type of data you want, otherwise it will return HTML.

before :each do
  request.env["HTTP_ACCEPT"] = 'application/json'
end

Upvotes: 1

Related Questions