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?
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 }