JohnTitor JohnTitor - 6 months ago 17
Ruby Question

Devise create user and login on test setup

app/controllers/categories_controller.rb:

class CategoriesController < ApplicationController
before_action :set_category, only: [:show, :edit, :update, :destroy]
before_action :authenticate_user!, except: [:index, :show]

# GET /categories
# GET /categories.json
def index
@categories = Category.all
end

# GET /categories/1
# GET /categories/1.json
def show
if session[:cart] then
@cart = session[:cart]
else
@cart = {}
end
end

# GET /categories/new
def new
if current_user.admin?
@category = Category.new
end
end

# GET /categories/1/edit
def edit
if current_user.admin?
end
end

# POST /categories
# POST /categories.json
def create
if current_user.admin?
@category = Category.new(category_params)

respond_to do |format|
if @category.save
format.html { redirect_to @category, notice: 'Category was successfully created.' }
format.json { render :show, status: :created, location: @category }
else
format.html { render :new }
format.json { render json: @category.errors, status: :unprocessable_entity }
end
end
end
end

# PATCH/PUT /categories/1
# PATCH/PUT /categories/1.json
def update
if current_user.admin?
respond_to do |format|
if @category.update(category_params)
format.html { redirect_to @category, notice: 'Category was successfully updated.' }
format.json { render :show, status: :ok, location: @category }
else
format.html { render :edit }
format.json { render json: @category.errors, status: :unprocessable_entity }
end
end
end
end

# DELETE /categories/1
# DELETE /categories/1.json
def destroy
if current_user.admin?
@category.destroy
respond_to do |format|
format.html { redirect_to categories_url, notice: 'Category was successfully destroyed.' }
format.json { head :no_content }
end
end
end

private
# Use callbacks to share common setup or constraints between actions.
def set_category
@category = Category.find(params[:id])
end

# Never trust parameters from the scary internet, only allow the white list through.
def category_params
params.require(:category).permit(:name, :desc)
end
end


In the controller above I'm preventing standard users to create, update or destroy categories by checking
if current_user.admin?
. But it's causing some problems in the tests.

test/controllers/categories_controller.rb:

require 'test_helper'

class CategoriesControllerTest < ActionController::TestCase
setup do
@category = categories(:one)
end

test "should get index" do
get :index
assert_response :success
assert_not_nil assigns(:categories)
end

test "should get new" do
get :new
assert_response :success
end

test "should create category" do
assert_difference('Category.count') do
post :create, category: { desc: @category.desc, name: @category.name }
end

assert_redirected_to category_path(assigns(:category))
end

test "should show category" do
get :show, id: @category
assert_response :success
end

test "should get edit" do
get :edit, id: @category
assert_response :success
end

test "should update category" do
patch :update, id: @category, category: { desc: @category.desc, name: @category.name }
assert_redirected_to category_path(assigns(:category))
end

test "should destroy category" do
assert_difference('Category.count', -1) do
delete :destroy, id: @category
end

assert_redirected_to categories_path
end
end


Because of the restriction, tests related with create, update or destroy are failing. I think I need to create an admin user and login on test setup. But I don't know how to do this.

I'm using Devise gem for users part of the app.

I'm running my tests with
rake test:functionals
. How can I simulate user create and login in my tests?

If you want to look to the whole project: https://github.com/mertyildiran/SCOR

Answer

You can use fixtures to create an admin user in the test database (you probably already have a test/fixtures/users.yml file), and Devise's Test helpers to sign in:

sign_in :user, users(:admin)

If your users are confirmable, remember to set a confirmed_at date.

Check out the Devise wiki article on testing Rails controllers.