NoMethodError:RSpec的未定义方法`get'

时间:2017-10-27 13:14:11

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

首先,这是我第一次使用ruby。此时,我正在为我的应用程序中名为Exporter的Controller创建测试。我想测试的控制器的方法是:

 let postParams = {
        id: "1"
    };
let headers = new Headers();
      headers.append('Content-Type', 'application/json');
      let options = new RequestOptions({
          params: postParams,
          headers: headers,
          withCredentials: true,
      });

      this.http.post("Enter your API URL",JSON.stringify(postParams), options)
      .map(res => res.json())
      .subscribe(data => { 
          console.log(data);
      },(err => {
          console.log(err);
      }));

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

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

2 个答案:

答案 0 :(得分:2)

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

步骤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阻止。

答案 1 :(得分:1)

我知道这不是你问题的答案。但是,既然你提到你是ruby的新手,我想我会指出你的代码可以简化和美化一点。

首先,您不需要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

现在,您hash放入的equipments_json只是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绝对有效,但不是现行惯例。 This是ruby的样式指南,如果有帮助的话。