首先,这是我第一次使用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
答案 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的样式指南,如果有帮助的话。