Being pretty new to testing, I was wondering how many of these tests make sense.
restaurants_controller_spec.rb
require 'spec_helper'
describe RestaurantsController do
describe "GET #index" do
before(:each) { get :index }
let(:restaurants) { create_list :restaurant, 5 }
it "populates @restaurants" do
expect(restaurants).to match_array assigns(:restaurants)
end
it "renders the :dailycious view" do
expect(response).to render_template :index
end
end
describe "GET #show" do
let(:restaurant) { create :restaurant }
before(:each) { get :show, id: restaurant }
it "assigns @restaurant" do
expect(restaurant).to eq assigns(:restaurant)
end
it "renders :show" do
expect(response).to render_template :show
end
end
describe "GET #new" do
before(:each) { get :new }
context "user signed in" do
before(:each) do
login_user
get :new
end
it "assigns @restaurant" do
expect(assigns(:restaurant)).to be_a_new(Restaurant)
end
it "renders :new" do
expect(response).to render_template :new
end
end
context "user signed out" do
it "redirects to login page" do
expect(response).to redirect_to new_user_session_path
end
end
end
end
support/controllers/controller_macros.rb
module Controllers
module ControllerMacros
def login_admin
@request.env["devise.mapping"] = Devise.mappings[:admin]
sign_in create(:confirmed_admin)
end
def login_user
@request.env["devise.mapping"] = Devise.mappings[:user]
sign_in create(:confirmed_user)
end
end
end
The new
test makes sense to me, since it behaves differently when logged in / logged out, but do the other ones make sense as well?
Especially the index
test seems superfluous. Doesn't this just check basic rails functionality? The index tests
of other controllers will surely share most of the same code. Isn't this a contradiction to the DRY-Principle?
My setup includes Rspec, Capybara & FactoryGirl.
Next to the explanations I'm glad if you review this code, maybe DRY things up etc. :)
Sidenote: I adjusted my code to this tutorial.