在 RSpec Capybara 测试中,您如何对自己的应用程序进行存根测试? API?
In RSpec Capybara tests how can you stub your own app's API?
我正在构建集成测试来测试视图的构建方式。该应用程序的架构是一个 Angular 前端与 Rails API 通信。
我想测试 Angular 中依赖于从服务器加载的数据的一项功能的行为。因此,我想存根该数据,以便我可以保证它被正确带回。我知道这在技术上不是集成测试,而是我正在测试的前端。
我希望能够做什么
describe "my front end feature" do
# This before block has the data I want to instruct my API to return
data = [
{"uid"=>"1", "last_seen_at"=>"2017-05-08T09:24:38"},
{"uid"=>"2", "last_seen_at"=>"2017-05-08T09:11:07"},
{"uid"=>"3", "last_seen_at"=>"2017-05-08T08:49:57"}
]
# NB this next line does not work...
WebMock.stub_request(:any, /\/my_api\/data_resource.*/).to_return(status: 200,
body: data.to_json)
end
it "Should load my stubbed data into the page" do
login member1
visit org_path(org)
# do my test to detect the appropriate content in the page
end
end
你怎么能存根自己的api?
我知道如何使用 webmock 存根外部 API 但是我想做的是存根我自己的 API 这样当水豚启动 firefox 时它会检索我的存根数据而不是点击应用程序。这可能吗?
正如您所提到的,这不是您在功能测试中真正应该做的事情。通常,您会在测试中使用固定装置或工厂配置数据,以便您的 API 实际上是 运行 而 returns 是您期望的数据。
但是,如果您坚持继续当前的计划,最好的解决方案(因为它不会修改您的应用程序代码,并且运行最接近功能测试的想法)是一个可编程的像 puffing-billy
- https://github.com/oesmith/puffing-billy 这样的代理。它类似于 WebMock,但用于从浏览器而不是应用程序内部发起的请求,并且旨在很好地集成到可用的 Capybara 驱动程序。
我正在构建集成测试来测试视图的构建方式。该应用程序的架构是一个 Angular 前端与 Rails API 通信。
我想测试 Angular 中依赖于从服务器加载的数据的一项功能的行为。因此,我想存根该数据,以便我可以保证它被正确带回。我知道这在技术上不是集成测试,而是我正在测试的前端。
我希望能够做什么
describe "my front end feature" do
# This before block has the data I want to instruct my API to return
data = [
{"uid"=>"1", "last_seen_at"=>"2017-05-08T09:24:38"},
{"uid"=>"2", "last_seen_at"=>"2017-05-08T09:11:07"},
{"uid"=>"3", "last_seen_at"=>"2017-05-08T08:49:57"}
]
# NB this next line does not work...
WebMock.stub_request(:any, /\/my_api\/data_resource.*/).to_return(status: 200,
body: data.to_json)
end
it "Should load my stubbed data into the page" do
login member1
visit org_path(org)
# do my test to detect the appropriate content in the page
end
end
你怎么能存根自己的api?
我知道如何使用 webmock 存根外部 API 但是我想做的是存根我自己的 API 这样当水豚启动 firefox 时它会检索我的存根数据而不是点击应用程序。这可能吗?
正如您所提到的,这不是您在功能测试中真正应该做的事情。通常,您会在测试中使用固定装置或工厂配置数据,以便您的 API 实际上是 运行 而 returns 是您期望的数据。
但是,如果您坚持继续当前的计划,最好的解决方案(因为它不会修改您的应用程序代码,并且运行最接近功能测试的想法)是一个可编程的像 puffing-billy
- https://github.com/oesmith/puffing-billy 这样的代理。它类似于 WebMock,但用于从浏览器而不是应用程序内部发起的请求,并且旨在很好地集成到可用的 Capybara 驱动程序。