ruby-on-rails - 无法从规范中的 API 检索记录
问题描述
当我运行时retrieve
显示以下错误:
参数数量错误(给定 0,预期为 1)(ArgumentError)
这里的一个人试图帮助我,但我认为我的问题很不方便,因为我真的无法解决它。我只需要这个来解决这个问题。
class Crud
include HTTParty
base_uri 'http://dummy.restapiexample.com/api/v1'
def create
nome = Faker::Name.first_name
salario = Faker::Number.decimal(l_digits: 4, r_digits: 2)
idade = Faker::Number.number(digits: 2)
#note, you should pass body as JSON string
body = { name: nome, salary: salario, age: idade }.to_json
headers = {
'Accept' => 'application/vnd.tasksmanager.v2',
'Content-Type' => 'application/json'
}
self.class.post('/create', body: body, headers: headers)
end
def retrieve(id)
self.class.get("/employee/#{ id }")
end
end
我正在尝试在规格中使用它
Consult employee Quando("é enviada a requisição para consultar o empregado") do
_manter_user.create
expect(_manter_user.create.code).to eq (200)
puts _manter_user.create.body
end
Então("posso visualizar as informações retornadas") do
expect(@manter_user.retrieve.code).to eq (200)
puts _manter_user.create.body
end
解决方案
问题在于多次调用create
,您需要将结果存储在变量中。您还需要在创建后立即检索实例 - 规范的顺序可能会发生变化,并且您可能会遇到在创建之前尝试检索的情况
Some spanish? text goes here do
created = manter_user.create
expect(created.code).to eq (200)
puts created.body
id = JSON.parse(created)['id']
retrieved = manter_user.retrieve(id)
expect(retrieved.code).to eq (200)
puts retrieved.body
end
推荐阅读
- tensorflow - Tensorflow R-CNN 网络小物体检测 - 并排的物体,一个分类正确,另一个完全漏掉
- excel - 如何从像自动填充这样的单元格生成公式
- java - 如何为 EntityManager 设置默认值
- image - cntk 图像解串器变换 mean_file
- google-chrome - 无头铬地理定位
- windows - Powershell 将文件复制到所有用户 LOCALAPPDATA 创建路径
- angular - Angular 6重定向到主页时,页面重定向但不加载
- batch-file - 将当前日期作为变量插入到 del 网络共享文件的批处理文件
- ruby-on-rails - 使用载波时如何在模型上设置“fog_public”值?
- javascript - Mechanicalsoup 带有交互式页面(无需打开浏览器)