【发布时间】:2018-04-02 16:59:55
【问题描述】:
我如何模拟类似于 python requests-mock 工作方式的特定 api 调用:
with requests_mock.mock() as m:
m.get('http://test.com', text='data')
requests.get('http://test.com').text
在此示例中,with 语句中对 http://test.com 的每次调用都会被模拟
例如,在长生不老药中,我有这个:
defmodule API do
# makes a call to an external API I dont want to test
def make_call do
...
end
end
defmodule Item do
alias API
# function I actually want to test
def build_request do
API.make_call
# stuff I want to test
end
end
假设我想测试build_request 模拟make_call
我试过这个包https://github.com/jjh42/mock,但是这个包的作用是覆盖整个API模块,例如make_call方法的模拟,但你也失去了API模块的所有其他功能,我不想那样。
我怎么能模拟那个调用?
在另一个例子中,我看到了https://hashrocket.com/blog/posts/mocking-api-s-with-elixir
# mocking a github api call directly
@github_api.make_request(:get, "/users/#{username}")
但这是同一个问题,它直接在测试中模拟请求,我的问题是当模拟需要在 inner 函数中完成时不是马上。
【问题讨论】: