I've been heads down in this and feel like I'm probably making a simple mistake, but I haven't been able to find any information on this problem.
I have some request specs for Rails 5 and, when I test for a redirect--but not for a rendered template--I get an error, undefined method 'response_code' for nil:NilClass
. The reason for this seems to be that @response
is nil
when the matcher is called (inside ActionDispatch::Assertions::ResponseAssertions code, not in my code). I am able to use cURL to make a request to the API and it returns a response as expected. The point where the error is returned is here (this is the ActionDispatch code):
def generate_response_message(expected, actual = @response.response_code)
"Expected response to be a <#{code_with_name(expected)}>,"\
" but was a <#{code_with_name(actual)}>"
.dup.concat(location_if_redirected).concat(response_body_if_short)
end
Notice the first line, where the default value of the actual
param is set to @response.response_code
.
Here's my test code:
RSpec.describe "Admin registrations", type: :request do
describe "new sign-up" do
subject { get new_admin_registration_path }
it "redirects to home" do
expect(subject).to redirect_to(new_admin_session_path)
end
end
end
The relevant lines in the test log are:
Started GET "/admins/sign_up" for 127.0.0.1 at 2018-07-05 10:44:05 -0700
Processing by Admins::RegistrationsController#new as HTML
Redirected to http://example.org/admins/sign_in
Completed 301 Moved Permanently in 18ms (ActiveRecord: 0.0ms)
Interestingly, when I use byebug to check the value of subject
, it does return a Rack::MockResponse object, so this is somehow not getting passed through.