I have the "users" and the "empresas", i used the scaffold command to generate the initial CRUD for "empresas" and after that i put the users id into the empresas and all the model need to, so what i want is to just the users that are the owner of the empresa can have acess to the "show", "update" and "destroy", i have a correct_user function and before action to apply that on the actions, everything works fine, unless the tests, i just apply the login into the original scaffold tests.
Empresas Controller
class EmpresasController < ApplicationController
before_action :set_empresa, only: [:show, :edit, :update, :destroy]
before_action :logged_in_user, only: [:update, :show, :index, :create, :destroy]
before_action :correct_user, only: [:show, :update, :destroy]
# GET /empresas
# GET /empresas.json
def index
@empresas = current_user.empresas.all
end
# GET /empresas/1
# GET /empresas/1.json
def show
end
# GET /empresas/new
def new
@empresa = Empresa.new
end
# GET /empresas/1/edit
def edit
end
# POST /empresas
# POST /empresas.json
def create
@empresa = current_user.empresas.build(empresa_params)
respond_to do |format|
if @empresa.save
format.html { redirect_to @empresa, notice: 'Empresa criada com sucesso.' }
format.json { render :show, status: :created, location: @empresa }
else
format.html { render :new }
format.json { render json: @empresa.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /empresas/1
# PATCH/PUT /empresas/1.json
def update
respond_to do |format|
if @empresa.update(empresa_params)
format.html { redirect_to @empresa, notice: 'Empresa alterada com sucesso.' }
format.json { render :show, status: :ok, location: @empresa }
else
format.html { render :edit }
format.json { render json: @empresa.errors, status: :unprocessable_entity }
end
end
end
# DELETE /empresas/1
# DELETE /empresas/1.json
def destroy
@empresa.destroy
respond_to do |format|
format.html { redirect_to empresas_url, notice: 'Empresa removida com sucesso.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_empresa
@empresa = current_user.empresas.find_by(id: params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def empresa_params
params.require(:empresa).permit(:nome_fantasia, :email, :razao_soc, :cnpj)
end
def correct_user
@empresa = current_user.empresas.find_by(id: params[:id])
redirect_to empresas_url if @empresa.nil?
end
end
Empresas_controller test
require 'test_helper'
class EmpresasControllerTest < ActionController::TestCase
setup do
@user = users(:carlos)
@empresa = empresas(:one)
@empresa.user_id = @user.id
end
test "should get index" do
log_in_as(users(:carlos))
get :index
assert_response :success
assert_not_nil assigns(:empresas)
end
test "should get new" do
get :new
assert_response :success
end
test "should create empresa" do
log_in_as(users(:carlos))
assert_difference('Empresa.count') do
post :create, empresa: {email: @empresa.email, nome_fantasia: @empresa.nome_fantasia, cnpj: @empresa.cnpj, razao_soc: @empresa.razao_soc }
end
assert_redirected_to empresa_path(assigns(:empresa))
end
test "should show empresa" do
log_in_as(users(:carlos))
get :show, id: @empresa
assert_response :success
end
test "should get edit" do
log_in_as(users(:carlos))
get :edit, id: @empresa
assert_response :success
end
test "should update empresa" do
log_in_as(users(:carlos))
patch :update, id: @empresa, empresa: {email: @empresa.email, nome_fantasia: @empresa.nome_fantasia, cnpj: @empresa.cnpj, razao_soc: @empresa.razao_soc }
assert_redirected_to empresa_path(assigns(:empresa))
end
test "should destroy empresa" do
log_in_as(users(:carlos))
assert_difference('Empresa.count', -1) do
delete :destroy, id: @empresa
end
assert_redirected_to empresas_path
end
end
The erros msgs
FAIL["test_should_destroy_empresa", EmpresasControllerTest, 2015-11-13 11:07:25 +0000]
test_should_destroy_empresa#EmpresasControllerTest (1447412845.23s)
"Empresa.count" didn't change by -1.
Expected: 2
Actual: 3
test/controllers/empresas_controller_test.rb:51:in `block in <class:EmpresasControllerTest>'
ERROR["test_should_get_edit", EmpresasControllerTest, 2015-11-13 11:07:25 +0000]
test_should_get_edit#EmpresasControllerTest (1447412845.33s)
ActionView::Template::Error: ActionView::Template::Error: First argument in form cannot contain nil or be empty
app/views/empresas/_form.html.erb:1:in `_app_views_empresas__form_html_erb___3156194878750235915_67173720'
app/views/empresas/edit.html.erb:4:in `_app_views_empresas_edit_html_erb___622806155688133729_66667680'
test/controllers/empresas_controller_test.rb:39:in `block in <class:EmpresasControllerTest>'
app/views/empresas/_form.html.erb:1:in `_app_views_empresas__form_html_erb___3156194878750235915_67173720'
app/views/empresas/edit.html.erb:4:in `_app_views_empresas_edit_html_erb___622806155688133729_66667680'
test/controllers/empresas_controller_test.rb:39:in `block in <class:EmpresasControllerTest>'
FAIL["test_should_show_empresa", EmpresasControllerTest, 2015-11-13 11:07:25 +0000]
test_should_show_empresa#EmpresasControllerTest (1447412845.35s)
Expected response to be a <success>, but was <302>
test/controllers/empresas_controller_test.rb:34:in `block in <class:EmpresasControllerTest>'
ERROR["test_should_update_empresa", EmpresasControllerTest, 2015-11-13 11:07:25 +0000]
test_should_update_empresa#EmpresasControllerTest (1447412845.36s)
ActionController::UrlGenerationError: ActionController::UrlGenerationError: No route matches {:action=>"show", :controller=>"empresas", :id=>nil} missing required keys: [:id]
test/controllers/empresas_controller_test.rb:46:in `block in <class:EmpresasControllerTest>'
test/controllers/empresas_controller_test.rb:46:in `block in <class:EmpresasControllerTest>'
61/61: [==================================================================================================================] 100% Time: 00:00:02, Time: 00:00:02
Finished in 2.21698s
61 tests, 166 assertions, 2 failures, 2 errors, 0 skips
Everything are working, but i realy want to learn tests
Aucun commentaire:
Enregistrer un commentaire