繁体   English   中英

NoMethodError:RSpec的未定义方法“ get”

[英]NoMethodError: undefined method `get' for RSpec

首先,这是我第一次使用红宝石。 目前,我正在为我的应用程序中名为Exporter的Controller创建测试。 我要测试的控制器的方法是这样的:

def export_as_json(equipments)
    equipments_json = []
    equipments.each {|equipment|
        equipment_json = {
            :id => equipment.id,
            :title => equipment.title,
            :description => equipment.description,
            :category => equipment.category_id
        }
        equipments_json << equipment_json
    }

    respond_to do |format|
      format.json { render :json =>equipments_json }
    end
end

因此,当我尝试使用以下方法为此方法创建请求时:

RSpec.describe ExporterController, type: :controller do
  get '/equipments/all', headers: { 'CONTENT_TYPE' => 'application/json' }, format: :json
  expect(response.response).to eq(200)
end

exporter_controller_test.rb文件中,我收到此错误:

NoMethodError: undefined method `get' for RSpec::ExampleGroups::ExporterController:Class

这几乎是每个人至少遇到一次的问题之一;)

步骤1:非常仔细地阅读错误消息

NoMethodError: undefined method 'get' for RSpec::ExampleGroups::ExporterController:Class

步骤2:记住措辞NoMethodError: undefined method get for RSpec::ExampleGroups::XXX:Class

步骤3:以实际例子解决

RSpec.describe ExporterController, "#index", type: :controller do
  it "should respond with status: 200" do
    get '/equipments/all', headers: { 'CONTENT_TYPE' => 'application/json' }, format: :json
    expect(response.response).to eq(200)
  end
end

您只是错过了it块。

我知道这不是您问题的答案。 但是,由于您提到您是红宝石的新手,所以我想指出一点,您的代码可以简化和修饰。

首先,您不需要执行equipments_json = [] ,然后执行equipments.each 这就是map的用途:

def export_as_json(equipments)
  equipments_json = equipments.map{|equipment| {
      :id => equipment.id,
      :title => equipment.title,
      :description => equipment.description,
      :category => equipment.category_id
    }
  }

  respond_to do |format|
    format.json { render :json =>equipments_json }
  end
end

现在,您要放入equipments_json hash只是equipment属性的一个子集。 因此,在此处使用slice获取所需的属性:

def export_as_json(equipments)
  equipments_json = equipments.map{|equipment| equipment.attributes.slice('id','title','description','category_id')}

  respond_to do |format|
    format.json { render :json =>equipments_json }
  end
end

map线仍然有些长,因此,也许将其放入do块中(就像您对each ):

def export_as_json(equipments)
  equipments_json = equipments.map do |equipment| 
    equipment.attributes.slice('id','title','description','category_id')
  end

  respond_to do |format|
    format.json { render :json =>equipments_json }
  end
end

就个人而言,我喜欢使用符号而不是字符串作为键,因此请使用with_indifferent_access以便使用符号:

def export_as_json(equipments)
  equipments_json = equipments.map do |equipment| 
    equipment.attributes.with_indifferent_access.slice(:id, :title, :description, :category_id)
  end

  respond_to do |format|
    format.json { render :json =>equipments_json }
  end
end

那条线又有点长,所以我想继续包装一下:

def export_as_json(equipments)
  equipments_json = equipments.map do |equipment| 
    equipment.
      attributes.
      with_indifferent_access.
      slice(:id, :title, :description, :category_id)
  end

  respond_to do |format|
    format.json { render :json =>equipments_json }
  end
end

现在,有一些不同的方法来获取所需的属性(例如,修改to_json )。 但是,这将完成工作。

希望对您有帮助,祝您好运!

PS:我刚刚在您的原始哈希中注意到,您正在执行以下操作:

:category => equipment.category_id

如果这不是一个错字,并且您确实想要使用category而不是category_id ,则可以执行以下操作:

def export_as_json(equipments)
  equipments_json = equipments.map do |equipment| 
    equipment.
      attributes.
      with_indifferent_access.
      slice(:id, :title, :description).
      merge!(category: equipment.category_id)
  end

  respond_to do |format|
    format.json { render :json =>equipments_json }
  end
end

另外,哈希的约定是做title: equipment.title :title => equipment.title绝对有效,但不是当前约定。 如果有帮助, 是红宝石的样式指南。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM