1

I've recently started converting some of the standard requests I have in my application to XHR (Ajax) requests. I'm in the process of updating my RSpec tests to reflect this but am encountering this error message:

Expected response to be a <:redirect>, but was <401>

My spec looks like:

describe "#index" do
  subject { xhr :get, :index, params }
  it { should redirect_to new_customer_session_path }
end

The test.log file entry looks like this:

(0.2ms) BEGIN
Processing by SocialNetworks::FacebookCommentsController#index as JS
Parameters: {"id"=>"84", "facebook_post_id"=>"17"}
  Completed 401 Unauthorized in 21ms
  Rendered text template (0.0ms)
(0.2ms) ROLLBACK

These are within the context of a user whom is not signed in (unauthorised). When I change the assertion to:

it { response.status.should == 401 }

I get this error:

Failure/Error: it { response.status.should == 401 }
    expected: 401
         got: 200 (using ==)

And respective, unhelpful, log entries...

(0.1ms) BEGIN
(0.0ms) BEGIN
(0.1ms) ROLLBACK
(0.1ms) ROLLBACK

I would like to assert that an unauthorised status is given when an unauthorised status has been given. Can anyone help?

Gav
  • 11,062
  • 7
  • 33
  • 35
  • I'm surprised that the status code from one run is 401 and the next run is 200 -- are you sure nothing else changed? What logging is available to show server responses for these cases? – sarnold Feb 20 '12 at 23:12
  • I've updated my question to include the unhelpful content of my log. I was puzzled by the change of status code. I can assert that I get 200, but I really wanted to capture the 401; since it is actually suppose to respond with Unauthorised. – Gav Feb 21 '12 at 10:52

1 Answers1

1

A response from a friend (@JonRowe) on Twitter yielded:

@gavinlaking Re: stack overflow, try changing the second test to include a call to subject eg.

it { subject; response.status.should == 401 }
Gav
  • 11,062
  • 7
  • 33
  • 35