gpt4 book ai didi

ruby-on-rails - 使用 capybara 和网络模拟 rails 应用程序进行测试

转载 作者:行者123 更新时间:2023-11-28 20:54:02 25 4
gpt4 key购买 nike


您好!我有一个 rails api 和一个前端 Backbone 。我的 api 调用外部 api 以获取一些资源。

我想在 capybara 中对完整应用程序进行集成测试。我正在使用网络模拟。

我的 spec_helper 的片段:

WebMock.disable_net_connect!(allow_localhost: true)

RSpec.configure do |config|
config.before(:each) do
stub_request(:get, "url.com/live/en/live/list.json").
with(headers: {'Accept'=>'*/*', 'User-Agent'=>'Ruby'}).
to_return(status: 200, body: body, headers: {})
end
(...)
end

def body
"{\"version\":\"9\",\"sports\":[{\"id\":101,\"title\":\"Football\"},{\"id\":100,\"title\":\"Tenis\"}]}"
end

我的功能测试:

feature 'sports displayment' do
scenario 'user access to the site' do
visit root_path

expect(page).to have_text("Football")
end
end

显示的错误是期望在“”中找到文本“Football”如果我在 Controller 上嵌入一个 binding.pry 它就不起作用(这意味着它不会通过它。

我确信该应用程序可以正常工作,所以这是一个测试问题。

应用程序链接 https://github.com/gerard-morera/bet_play

--------编辑--------

我一直在尝试一些合理的新事物,但仍然行不通。我添加了 selenium,因为默认 capybara 潜水员不支持 js,我添加了 wait_for_ajax 方法,以确保 capybara 正在等待 ajax 请求,并且我还为这种情况启用了 js:

require 'rails_helper'

feature 'sports displayment' do

before(:all) do
Capybara.current_driver = :selenium
end

scenario 'user access to the site', js: true do
visit '/'

wait_for_ajax

expect(page).to have_text("Football")
end
end

最佳答案

你应该试试

expect(page).to have_content("Football")

关于ruby-on-rails - 使用 capybara 和网络模拟 rails 应用程序进行测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33319317/

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