期待<“编辑”>但使用< []>进行渲染

时间:2014-03-11 08:47:13

标签: ruby-on-rails rspec ruby-on-rails-4 rspec-rails

大家好我正在测试我的app控制器,我遇到了问题。我对更新操作的测试失败了:

describe "PUT #update" do 
    before :each do 
      @car_service = create(:car_service)
    end

    it "locates the requested @message" do 
      put :update, id: @car_service, car_addition: attributes_for(:car_service)
      assigns(:car_addition).should eq(@car_service)
    end

    context "valid attributes" do 
      it "changes @car_service's attributes" do 
        put :update, id: @car_service, car_addition: attributes_for(:car_service, name: "Test")
        @car_service.reload
        @car_service.name.should eq("Test")
      end

      it "redirects to the updated message" do 
        put :update, id: @car_service, car_addition: attributes_for(:car_service)
        should redirect_to admin_car_additions_url
      end
    end

    context "invalid attributes" do 
      it "does not change @car_addition's attributes" do 
        put :update, id: @car_service, car_addition: attributes_for(:car_service, name: nil)
        @car_service.reload
        @car_service.name.should_not be_nil
      end

      it "re-renders the edit method" do 
        put :update, id: @car_service, car_addition: attributes_for(:car_addition)

        should render_template :edit

      end
    end
  end

当我运行此测试时,只有一个测试未通过(“重新呈现编辑方法”)并抛出以下错误:

Failure/Error: should render_template('edit')
       expecting <"edit"> but rendering with <[]>
     # ./spec/controllers/admin/car_additions_controller_spec.rb:100:in `block (4 levels) in <top (required)>

我的控制器看起来像这样:

module Admin
  class CarAdditionsController < ApplicationController
    include Admin::BaseController

    load_and_authorize_resource

    add_breadcrumb I18n.t('car_additions.car_addition.home'), :admin_root_path
    add_breadcrumb I18n.t('car_additions.car_additions'), :admin_car_additions_path

    def index
    end

    def new
      add_breadcrumb t('car_additions.car_addition.new')
    end

    def edit
      add_breadcrumb t('car_additions.car_addition.edit')
    end

    def create
      if @car_addition.save
        flash[:notice] = t("car_additions.created")
        redirect_to action: :index
      else
        add_breadcrumb t('car_additions.car_addition.new')
        render :new
      end
    end

    def update
      if @car_addition.update(car_addition_params)
        flash[:notice] = t("car_additions.updated")
        redirect_to action: :index
      else
        render :edit
      end
    end

    def destroy
      @car_additon.destroy
      flash[:error] = t("car_additions.destroy")
      redirect_to action: :index
    end

    private
    def car_addition_params
      params.require(:car_addition).permit(:name, :type, :image,
        :image_cache, :remove_image)
    end
  end
end

我正在使用设计和CanCan进行授权。请帮忙。


我通过attributes_for(:car_addition),因为这不是有效的属性。当我把它改成:  attributes_for(:car_addition, name: nil)它仍无效...

1 个答案:

答案 0 :(得分:1)

您应该使用render_views方法,以便在规范中呈现您的观看次数:

describe "PUT #update" do
  render_views
  # ...
end