gpt4 book ai didi

ruby-on-rails - 什么/可以伪造 : do?

转载 作者:太空宇宙 更新时间:2023-11-03 18:09:58 25 4
gpt4 key购买 nike

我继承了这段代码,我知道这是为假的 api 调用创建了一个 stub 。我不明白它是如何工作的。它可以只返回 JSON 吗?我可以将响应设置为简单的 200 成功吗?有这方面的文档吗?

class GuessTransaction < ActiveRestClient::Base
request_body_type :json

get :all, '/transaction', fake: [{last_name:"Durdan", first_name:"Tyler"}]
get :find, '/transaction/:id', fake: {id: "1", first_name:"Tyler", last_name: "Durdan"}
post :create, '/transaction', fake:->(request) { {id: 12345 } }
end

最佳答案

如果您阅读 active-rest-client 的文档你可以找到这个:

Faking Calls

There are times when an API hasn't been developed yet, so you want to fake the API call response. To do this, you can simply pass a fake option when mapping the call containing the response.

class Person < ActiveRestClient::Base
get :all, '/people', fake: [{first_name:"Johnny"}, {first_name:"Bob"}]
end

You may want to run a proc when faking data (to put information from the parameters in to the response or return different responses depending on the parameters). To do this just pass a proc to :fake:

class Person < ActiveRestClient::Base
get :all, '/people', fake: ->(request) { {result: request.get_params[:id]} }
end

关于ruby-on-rails - 什么/可以伪造 : do?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36225544/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com