RSpec奇怪的行为:没有看到控制器动作

时间:2017-02-08 12:52:50

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

我有控制器的层次结构

Backend::ProductsController < Backend::ModelController < Backend::BackendController < ApplicationController

创建在Backend :: ModelController中定义的动作

在开发和生产模式下,即使是在标准轨道测试中,每件事情也都很好。

使用rspec capybara selenium和puma或WebRick编写了rspec特性,集成测试。

当我按照规范提交表格时,我得到了

<AbstractController::ActionNotFound: The action 'create' could not be found for Backend::ProductsController>

当我向Backend :: ProductsController

添加动作防御时
def create
  super
end

它也适用于Rspec。

所以我的问题是:为什么rspec在Backend :: ModelController中看不到创建动作?

app/controllers/backend/model_controller.rb

class Backend::ModelController < Backend::BackendController
  before_action :set_object,      only: [:show, :edit, :update, :destroy, :convert]
  respond_to :html, :js, :json
  attr_reader :model, :model_name, :index_path

  include Backend::FormsHelper
  include ActionView::Helpers::UrlHelper

...    
  def create
    @object = model.new(request_params)

    if @object.save
      flash[:success] = I18n.t("status.created", resource: I18n.t("#{model_name}.one"), title: "<strong>#{@object}</strong>")
    end

    instance_variable_set("@#{model_name}", @object)
    index_path = eval("#{model_name.pluralize}_path(session['#{model_name}_index_params'])")
    respond_with(@object, location: index_path, action: "#{model_name}")
  end
...
end

app/controllers/backend/products_controller.rb

class Backend::ProductsController < Backend::ModelController
  before_action only: [:new,  :create] { @tab_list = "generals descriptions parameters" }
  before_action only: [:edit, :update] { @tab_list = "generals descriptions parameters banners specifications competitions photos attachments redirections" }

  def new
    flash[:info] = t('product.save_required')
    super
    flash.clear
  end

def 
  super
end

  def edit
    @product.control_specifications
    super
  end

  def update
    old_cache_key = @product.cache_key
    super
    if params[:novelty] == "true"
      @product.update_columns(created_at: Time.now)
      @product.update_columns(top_sale: false)
    end
    @product.touch_gently if old_cache_key == @product.cache_key
  end


end

rspec log

2017-02-08 14:20:02 +0200: Rack app error handling request { POST /backend/products }
#<AbstractController::ActionNotFound: The action 'create' could not be found for Backend::ProductsController>
/home/anton/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/actionpack-5.0.1/lib/abstract_controller/base.rb:121:in `process'
/home/anton/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/actionview-5.0.1/lib/action_view/rendering.rb:30:in `process'
/home/anton/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/actionpack-5.0.1/lib/action_controller/metal.rb:190:in `dispatch'
/home/anton/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/actionpack-5.0.1/lib/action_controller/metal.rb:262:in `dispatch'
...

0 个答案:

没有答案