【问题标题】:Testing with capybara and web mock rails application使用 capybara 和 web mock rails 应用程序进行测试
【发布时间】:2015-10-24 14:32:58
【问题描述】:

你好! 我在主干上有一个 rails api 和一个前端。 我的 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

显示的错误是expected to find text "Football" in "" 如果我在控制器上嵌入了 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

【问题讨论】:

标签: ruby-on-rails testing rspec mocking capybara


【解决方案1】:

你应该试试

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-18
    • 2016-04-28
    • 2012-10-21
    相关资源
    最近更新 更多