我在使用Capybara& amp;测试一个相当简单的规范时遇到了问题。 Rails 4应用程序中的恶作剧者。
规范如下:
feature 'Creating user groups', :devise, js: true do
let(:panel) { create(:panel) }
let(:admin) { create(:user, :admin) }
before do
login_as(admin)
visit admin_panel_user_groups_path(panel)
click_link 'New user group'
end
context 'successfully' do
it 'admin can create a new user group' do
fill_in 'User group', with: 'Some group'
click_button 'Save'
expect(page).to have_content('Your user group has been saved correctly')
end
end
end
规范最终调用此create
方法,该方法返回带有format.js
的json响应(这就是我使用js: true
运行上述规范的原因):
class Admin::UserGroupsController < AdminController
respond_to :html, :json, :js, :csv
before_action :set_panel
def create
respond_to do |format|
@user_group = UserGroup.new(user_group_params.merge({panel_id: @panel.id}))
if @user_group.save
@user_group.refresh!
format.js do
render json: {
responseType: 'modelSaved',
object: @user_group
}.to_json, status: 200
end
else
format.js do
render json: {
responseType: 'error',
object: @user_group
}.to_json, status: 500
end
end
end
end
end
我已经在rails_helper.rb
fie:
require 'capybara/poltergeist'
RSpec.configure do |config|
config.include Warden::Test::Helpers, type: :feature
config.after(type: :feature) { Warden.test_reset! }
Capybara.javascript_driver = :poltergeist
end
但是当我运行规范时,我收到以下错误消息:
Failure/Error: visit admin_panel_user_groups_path(panel)
WebMock::NetConnectNotAllowedError:
Real HTTP connections are disabled. Unregistered request: GET http://127.0.0.1:54553/__identify__ with headers {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent'=>'Ruby'}
You can stub this request with the following snippet:
stub_request(:get, "http://127.0.0.1:54553/__identify__").
with(:headers => {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent'=>'Ruby'}).
to_return(:status => 200, :body => "", :headers => {})
registered request stubs:
stub_request(:get, "/http:\/\/maps.googleapis.com\/maps\/api\/geocode\/json?components=country:US|postal_code:\d+/")
stub_request(:get, "https://freegeoip.net/json/123.123.123.123").
with(:headers => {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent'=>'Ruby'})
我相信这种情况正在发生,因为规范中不允许外部连接(这很好)。但是,在集成测试中存根外部请求是否有意义?我是错误地配置了Poltergeist还是错误地考虑了这个测试?
提前致谢!
答案 0 :(得分:0)
在运行功能测试时,你真的不应该启用WebMock(它不能存储或阻止&#34;浏览器发出的请求&#34;你正在使用,PhantomJS,无论如何它并没有真正保护你免受传出连接的影响)。因此,您需要在运行功能测试时允许连接
WebMock.allow_net_connect!
或者至少允许连接到AUT,在这种情况下,在127.0.0.1上运行
WebMock.disable_net_connect!(allow_localhost: true)