Skip to content

Instantly share code, notes, and snippets.

@MatthewRDodds
Created February 23, 2016 17:17
Show Gist options
  • Save MatthewRDodds/59d444d491eab384fb5c to your computer and use it in GitHub Desktop.
Save MatthewRDodds/59d444d491eab384fb5c to your computer and use it in GitHub Desktop.
Skipping Authentication For Rspec Controllers in a Json API
RSpec.configure do |config|
  config.before(:each, type: :controller) do |example|
    unless example.metadata[:skip_login_mock]
      allow_any_instance_of(ApiController).to receive(:restrict_access)
        .and_return(true)
    end
  end
end

This skips the restrict_access action defined in the ApiController which serves to render an error unless a valid auth_token is provided along with the request.

So it is good to mock this for most specs, but allow the spec to override skipping (if for example we want to test that auth is happening correctly). So the stubbing can be overridden by defining the spec as follows:

it 'tests somethiing', skip_login_mock: true do
  # ...
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment