UnderTaker
UnderTaker

Reputation: 893

Access request and response headers in rspec

I am testing my model methods, is there anyway i can access request or response headers.

require 'spec_helper'

describe Project do

  it "can find an Project that this user belongs to" do 
    project = Project.find( id: '22', authorization: @auth ) // Rest API Call        
    expect(response.code).to eq(200);
  end

end

When i try to access my response.code, i get undefined method/variable response. How can i access my response headers.

Update My Project Model:

class Project < ActiveRestClient::Base  
  base_url "https://domainname.com"

  get :find, '/project/:id/'
end

Upvotes: -1

Views: 1398

Answers (1)

Taryn East
Taryn East

Reputation: 27747

You are trying to test something that is just not available to you - ie the code that underlies the ActiveRestClient interface. It's not code in your model, so you shouldn't be testing it in your model-spec. You are testing things that are too deeply embedded, rather than what your code actually should be doing. This is a bit like testing that you can drive a car - by checking that the pistons are moving up and down in the engine... instead of looking out the window and making sure you aren't crashing into things.

If ActiveRestClient has its own test suite - then you can expect that it Just Works (eg that you get a 200 response when you successfully return something). you don't need to test that in your model code. Your model code should test just the functionality that you provide. eg test that your model has an ActiveRestClient-based "find" method on it... and assume (at this level) that it just works.

If you want to test end-to-end functionality... put that in your feature specs instead.

Upvotes: 1

Related Questions