0

I am looking for clarification and an understanding on how to effectively test my controllers with Rspec, I don't want to write tests that are not testing the potential issues at hand.

My scenario is as follows.

I am using Active Admin to create a Category, to do so you must obviously be logged into Active Admin.

What I want to ensure is that

1) A logged in user can create a Category

2) A Category cannot be created if you are not logged in

3) Attempts to create a Category outside of active admin are met with a 404 template

So what i have so far (and i really want to check i haven't gone over the top or performing unnecessary tests) is as follows.

spec/controllers/categories_controller_spec.rb

require 'rails_helper' include Warden::Test::Helpers # Ensure 404 pages are returned when requesting URLS RSpec.describe CategoriesController, type: :request do describe 'Routes' do context 'All CRUD actions render 404' do it '#create' do post '/categories' expect(response.status).to eq(404) expect(response).to render_template(:file => "#{Rails.root}/public/404.html.erb") end # All other actions here end end end RSpec.describe Admin::CategoriesController, type: :request do describe 'No Authorised Login' do context 'All CRUD actions redirect correctly' do it 'redirects when accessing #index' do get '/admin/categories' expect(response.status).to eq(302) expect(response).to redirect_to(admin_root_path + '/login') end # All other actions here end end end # Ensure actions in admin can be carried out if logged in RSpec.describe Admin::CategoriesController, type: :request do before(:each) do @user = FactoryGirl.create(:admin_user) login_as @user end after(:each) do @user.destroy end describe 'Authorised Login' do context 'All CRUD actions perform as expected' do it 'navigates to Categories #index' do get '/my_admin_panel/categories' expect(response.status).to eq(200) expect(response).to render_template(:index) end # All other actions here end end 

spec/routing/categories_routing.spec

RSpec.describe CategoriesController, type: :routing do describe 'Routes' do it 'does not get #index' do expect(get: '/categories').to route_to( controller: 'application', action: 'raise_not_found', unmatched_route: 'categories' ) end end end 

Should I be testing post /categories without supplying params, is that a wasted test? Am I over complicating what should be a simple set of tests ?

1 Answer 1

1

This is a judgement/style question and, as such, is not ideal for the StackOverflow format. That said, I don't think your testing is over the top. Some other thoughts:

  • Some people choose treat their controller tests as integration tests.
  • You can take advantage of RSpec's shared examples to DRY up your tests
  • In the default RSpec configuration, the database will be cleaned after each test, so you don't need to explicitly destroy the ActiveRecord objects you create if it's the database you're worried about
  • In general, I think testing behavior for programmatic actions you don't expect to happen (e.g. posting to undefined routes) is worthwhile unless you are trying to test specific error handling code
Sign up to request clarification or add additional context in comments.

2 Comments

I appreciate that this could be interpreted as a judgement/style question, so i thank you for taking the time to answer the question. you mentioned In the default RSpec configuration, the database will be cleaned after each test, I take it i need to set this in the config?
If you're using the Rails RSpec gem and install it per the instructions (see relishapp.com/rspec/rspec-rails/docs/transactions), then each of your tests will be executed within a database transaction without the need for further configuration.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.