Ruby on rails 更新、显示和销毁DOS的控制器测试';不适用于引用的类

Ruby on rails 更新、显示和销毁DOS的控制器测试';不适用于引用的类,ruby-on-rails,ruby,unit-testing,testing,Ruby On Rails,Ruby,Unit Testing,Testing,我有“users”和“empresas”,我使用scaffold命令为“empresas”生成初始CRUD,然后我将用户id放入empresas,所有模型都需要,所以我想要的是只有empresa所有者的用户可以访问“show”、“update”和“destroy”,我有一个正确的用户函数,在将其应用于操作之前,一切正常,除非进行测试,否则我只是将登录应用到原始的scaffold测试中 Empresas控制器 class EmpresasController

我有“users”和“empresas”,我使用scaffold命令为“empresas”生成初始CRUD,然后我将用户id放入empresas,所有模型都需要,所以我想要的是只有empresa所有者的用户可以访问“show”、“update”和“destroy”,我有一个正确的用户函数,在将其应用于操作之前,一切正常,除非进行测试,否则我只是将登录应用到原始的scaffold测试中

Empresas控制器

class EmpresasController
Empresas_控制器测试

需要“测试助手”
类EmpresasControllerTest
埃罗斯msgs酒店

FAIL[“测试应该摧毁”empresa“,EmpresasControllerTest,2015-11-13 11:07:25+0000]
测试应销毁empresa empresa控制器测试(1447412845.23s)
“Empresa.count”没有改变-1。
预期:2
实际:3
测试/控制器/empresas_控制器_测试。rb:51:in‘block in’
错误[“测试应该得到编辑”,EmpresasControllerTest,2015-11-13 11:07:25+0000]
测试应获得编辑EMPRESSOControllerTest(1447412845.33s)
ActionView::Template::Error:ActionView::Template::Error:表单中的第一个参数不能包含nil或为空
app/views/empresas/_form.html.erb:1:在`_app_views_empresas_form_html_erb_31561948750235915_67173720'中
app/views/empresas/edit.html.erb:4:in`_app_views_empresas_edit_html_erb_622806155688133729_66667680'
测试/控制器/empresas\u控制器\u测试。rb:39:in'block in'
app/views/empresas/_form.html.erb:1:在`_app_views_empresas_form_html_erb_31561948750235915_67173720'中
app/views/empresas/edit.html.erb:4:in`_app_views_empresas_edit_html_erb_622806155688133729_66667680'
测试/控制器/empresas\u控制器\u测试。rb:39:in'block in'
失败[“测试应该显示测试”,测试控制测试,2015-11-13 11:07:25+0000]
测试应显示empresa empresa控制器测试(1447412845.35s)
预期响应为a,但为
测试/控制器/empresas\u控制器\u测试。rb:34:in'block in'
错误[“测试应更新测试”,EMPRESSCONTROLERTEST,2015-11-13 11:07:25+0000]
测试应更新empresa EMPRESSOControllerTest(1
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
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
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
@empresa.save
@empresa.user_id = @user.id