[英]NoMethodError: undefined method `get' for RSpec
First of all, this is my first experience with ruby. 首先,这是我第一次使用红宝石。 At this moment, I'm creating tests for the a Controller called Exporter in my application.
目前,我正在为我的应用程序中名为Exporter的Controller创建测试。 The method of the Controller I want to test is this:
我要测试的控制器的方法是这样的:
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
So, when I try to create a request for this method using this: 因此,当我尝试使用以下方法为此方法创建请求时:
RSpec.describe ExporterController, type: :controller do
get '/equipments/all', headers: { 'CONTENT_TYPE' => 'application/json' }, format: :json
expect(response.response).to eq(200)
end
inside the exporter_controller_test.rb
file I'm receiving this error: 在
exporter_controller_test.rb
文件中,我收到此错误:
NoMethodError: undefined method `get' for RSpec::ExampleGroups::ExporterController:Class
This is one of the problems pretty much every one runs into at least once ;) 这几乎是每个人至少遇到一次的问题之一;)
Step 1: Read the error message very carefully 步骤1:非常仔细地阅读错误消息
NoMethodError: undefined method 'get' for RSpec::ExampleGroups::ExporterController:Class
Step 2: Remember the wording NoMethodError: undefined method get for RSpec::ExampleGroups::XXX:Class
步骤2:记住措辞
NoMethodError: undefined method get for RSpec::ExampleGroups::XXX:Class
Step 3: Solve it by making it an actual example 步骤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
You were simply missing the it
block. 您只是错过了
it
块。
I know this is not an answer to your question. 我知道这不是您问题的答案。 But, since you mentioned that you're new to ruby, I thought I would point out that your code could be simplified and prettified a bit.
但是,由于您提到您是红宝石的新手,所以我想指出一点,您的代码可以简化和修饰。
First, you don't need to do equipments_json = []
and then equipments.each
. 首先,您不需要执行
equipments_json = []
,然后执行equipments.each
。 That's what map
is for: 这就是
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
Now, that hash
you're putting into equipments_json
is just a subset of equipment
's attributes. 现在,您要放入
equipments_json
hash
只是equipment
属性的一个子集。 So, use slice
there to get the attributes you want: 因此,在此处使用
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
That map
line is still a little long, so, maybe put it into a do
block (like you had with each
): 该
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
And personally, I like using symbols instead of strings as my keys, so use with_indifferent_access
so that you can use symbols: 就个人而言,我喜欢使用符号而不是字符串作为键,因此请使用
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
That line got a little to long again, so I think I would go ahead and wrap it: 那条线又有点长,所以我想继续包装一下:
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
Now, there are some different ways to get those attributes you want (eg, modifying to_json
). 现在,有一些不同的方法来获取所需的属性(例如,修改
to_json
)。 But, this will get the job done. 但是,这将完成工作。
Hope that helps and good luck! 希望对您有帮助,祝您好运!
PS: I just noticed in your original hash, you're doing: PS:我刚刚在您的原始哈希中注意到,您正在执行以下操作:
:category => equipment.category_id
if that's not a typo and you really want category
instead of category_id
, then you could do something like: 如果这不是一个错字,并且您确实想要使用
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
Also, the convention for hashes is to do title: equipment.title
. 另外,哈希的约定是做
title: equipment.title
。 :title => equipment.title
absolutely works, but is not the current convention. :title => equipment.title
绝对有效,但不是当前约定。 This is a style guide for ruby, in case it helps. 如果有帮助, 这是红宝石的样式指南。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.