-1

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
Taryn East
  • 27,486
  • 9
  • 86
  • 108
UnderTaker
  • 853
  • 3
  • 13
  • 22
  • [Request](http://stackoverflow.com/questions/9654465/how-to-set-request-headers-in-rspec-request-spec) and [response](http://codereview.stackexchange.com/questions/17860/rspec-integration-tests-for-a-simple-rails-api). But this won't help you i guess, cause this is not a request. You probably test model, not controller. – zishe Aug 10 '14 at 19:42
  • @zishe: If you notice the link for response... He is accessing the PlayersController not the PlayersModel methods. – UnderTaker Aug 10 '14 at 19:47
  • @zishe: You didn't any??? – UnderTaker Aug 10 '14 at 19:51
  • I don't see a request in this code. You should show your model, maybe it has. – zishe Aug 10 '14 at 19:53
  • @zishe: Here is my model. http://pastie.org/private/0o18pnqmto2drzymxwktw – UnderTaker Aug 10 '14 at 19:56
  • Ok, i got it, you should mention that you are using `ActiveRestClient` gem. It's hard to say how it could be tested. You can ask it on gem issues page. – zishe Aug 10 '14 at 20:15
  • Can you tell me on what question i should ask there?. I mean exact point to perfect – UnderTaker Aug 10 '14 at 20:17
  • [Create the issue](https://github.com/whichdigital/active-rest-client/issues) with a question label. – zishe Aug 10 '14 at 20:20

1 Answers1

1

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.

Taryn East
  • 27,486
  • 9
  • 86
  • 108