Rails功能规格:调用外部Google字体API,但我不知道在哪里

时间:2017-02-01 11:45:05

标签: ruby-on-rails rspec phantomjs capybara google-font-api

当我在没有互联网连接的情况下运行我的Rails功能规范时,测试失败的原因如下:

  3) Creating user textarea fullscreen feature of "about" textarea shows the fullscreen toggler on hover
     Failure/Error: visit new_user_path

     Capybara::Poltergeist::StatusFailError:
       Request to 'http://127.0.0.1:65001/en/users/new' failed to reach server, check DNS and/or server status - Timed out with the following resources still waiting http://fonts.googleapis.com/css?family=Fredericka+the+Great
     # /Users/josh/.rvm/gems/ruby-2.2.6@a4aa2/gems/poltergeist-1.13.0/lib/capybara/poltergeist/browser.rb:376:in `command'
     # /Users/josh/.rvm/gems/ruby-2.2.6@a4aa2/gems/poltergeist-1.13.0/lib/capybara/poltergeist/browser.rb:35:in `visit'
     # /Users/josh/.rvm/gems/ruby-2.2.6@a4aa2/gems/poltergeist-1.13.0/lib/capybara/poltergeist/driver.rb:97:in `visit'
     # /Users/josh/.rvm/gems/ruby-2.2.6@a4aa2/gems/capybara-2.12.0/lib/capybara/session.rb:252:in `visit'
     # /Users/josh/.rvm/gems/ruby-2.2.6@a4aa2/gems/capybara-2.12.0/lib/capybara/dsl.rb:52:in `block (2 levels) in <module:DSL>'
     # ./spec/features/users/new_spec.rb:137:in `block (3 levels) in <top (required)>'

我怀疑这是由我引用的谷歌字体引起的,但我已经在本地下载了字体并在我的SASS中加载它:

@font-face
  font-family: 'Fredericka the Great'
  font-style: normal
  font-weight: 400
  src: local('Fredericka the Great'), local('FrederickatheGreat'), asset-url('assets/fredericka_the_great.woff2') format('woff2')
  unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02C6, U+02DA, U+02DC, U+2000-206F, U+2074, U+20AC, U+2212, U+2215

所以我在代码中没有看到http://fonts.googleapis.com的任何引用。在整个项目中搜索这个确切的字符串也不会产生任何结果。

如何防止此问题?我不希望PhantomJS与任何非本地资源联系,因为它会减慢测试速度。

2 个答案:

答案 0 :(得分:1)

如果您不希望Poltergeist / PhantomJS访问127.0.0.1之外的任何资源,请使用Poltergeist提供的白名单功能。注册:poltergeist驱动程序时,请指定:url_whitelist选项

Capybara.register_driver(:poltergeist) do |app|
  Capybara::Poltergeist::Driver.new(app, url_whitelist: ['http://127.0.0.1'])
end

答案 1 :(得分:0)

我发现了问题:我的application.css在测试时未正确重新编译,因此需要旧版本。

执行手动$ rake assets:precompile后,不再调用google font api源。