Capybara webkit在RSpec测试中将我重定向到example.com

时间:2015-07-07 14:13:47

标签: ruby-on-rails ruby rspec capybara capybara-webkit

我的ruby on rails应用程序有一个RSPec测试套件,如下所示:

scenario "Buyer visits people you follow page" do
   ...do stuff
  end

  scenario "Buyer logs out" do
   ...do stuff
  end

  scenario "Buyer creates a new event", :js => true do
   ...do stuff
  end

在每个场景运行之前,运行后台sign_in_as模块:

module SignInHelpers
  def sign_in_as(user = FactoryGirl.create(:user))
    visit welcome_page_url
    save_and_open_page
    fill_in :user_email, with: user.email
    fill_in :user_password, with: "password"
    click_button "Log in"
  end
end

设置的所有场景:js => true工作正常。场景“买方创建一个新事件”javascript很重要,所以我想使用我的webkit来启用javascript和capybara page.execute_script方法......测试失败了:

Buyer Features Buyer creates a new event
     Failure/Error: sign_in_as
     Capybara::ElementNotFound:
       Unable to find field :user_email

此代码在所有方案之前运行并且在之前的8个测试中有效,那么为什么它现在会失败?

当我save_and_open_page时,我意识到测试是在example.com上进行的!为什么是这样?我觉得如果我发现原因 - 问题得到解决!

这是我的spec_helper.rb:

ENV["RAILS_ENV"] ||= "test"
require File.expand_path("../../config/environment", __FILE__)
require "rspec/rails"

Capybara.javascript_driver = :webkit

Capybara::Webkit.configure do |config|
  # Enable debug mode. Prints a log of everything the driver is doing.
  config.debug = false

  # By default, requests to outside domains (anything besides localhost) will
  # result in a warning. Several methods allow you to change this behavior.


  # Allow pages to make requests to any URL without issuing a warning.
  config.allow_unknown_urls

  # Allow a specifc domain without issuing a warning.
  config.allow_url("localhost")

  # Timeout if requests take longer than 5 seconds
  config.timeout = 10

  # Don't raise errors when SSL certificates can't be validated
  config.ignore_ssl_errors

end



Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
RSpec.configure do |config|

  config.use_transactional_fixtures = true
  config.mock_with :rspec
  config.expect_with :rspec do |expectations|

    expectations.include_chain_clauses_in_custom_matcher_descriptions = true
  end

  config.mock_with :rspec do |mocks|

    mocks.verify_partial_doubles = true
  end


=begin

  config.filter_run :focus
  config.run_all_when_everything_filtered = true


  config.example_status_persistence_file_path = "spec/examples.txt"


  config.disable_monkey_patching!


  if config.files_to_run.one?

    config.default_formatter = 'doc'
  end


  config.profile_examples = 10


  config.order = :random


  Kernel.srand config.seed
=end
end

我认为这个问题与这里的某些事情有关,或者这里缺少的东西,但是我无法解决这个问题!

**请求的路线文件**

Rails.application.routes.draw do
  resources :invitations

  post 'utilities/getUserLocation', to: 'utilities#getUserLocation'

  resources :geo_ip_request

  resources :events

  get 'sessions/quick', to: 'sessions#quickBuy', as: "quick_buy"
  get 'sessions/:id', to: 'sessions#show', as: "session"


  post 'sessions/quick', to: 'sessions#getSession'
  post 'sessions/checkin', to: 'sessions#checkin'

  resources :tests

  get '/search', to: 'search#search', as: "search_search"

  get 'welcome/index', as: "welcome_page"



  #get 'sessions/show/:id', to: 'sessions#show', as: "session_show"

  devise_for :users, path: "users", path_names: { sign_in: 'login', sign_out: 'logout', password: 'secret', confirmation: 'verification', unlock: 'unblock', sign_up: 'new/(:invitation_token)' }, controllers: {sessions: 'users/sessions', registrations: 'users/registrations',  :omniauth_callbacks => "users/omniauth_callbacks"}


#resources :users

devise_scope :user do 
 get '/users/:id', to: 'users#show', as: 'user'
end

get '/users/eventOwner/:id', to: 'users#showEventOwner', as: 'userShowEventOwner'

post  'events/:id', to: 'events#buyTicket'

get 'users/:id/connect', to: 'users#connect', as: "connect_users"

get 'users/confirm', to: 'users#confirm', as: "confirm_users"

get 'users/followers/:id', to:'users#show_followers', as: "show_followers"
get 'users/followees/:id', to:'users#show_followees', as: "show_followees"
post 'users/follow/:id', to: 'users#follow_user', as:"follow_user" 


   #add route to show users favourites collection

   get 'users/favourites/:id', to: 'users#show_favourites', as: 'show_favourites'
   post 'users/favourites/add', to: 'users#add_favourite', as: 'add_favourite'


   patch 'attendees', to: 'attendees#update', as: "update_attendees"

 #get 'users/:id', to: 'users/users#show'
 get 'charges/error', to: 'charges#error', as: "payment_error"
 get 'charges/:id', to: 'charges#show'
 post 'charges/:id', to: 'charges#show'
 post 'charges/', to: 'charges#show'
 get 'listings/:id', to: 'listings#index', as: "listings_index"
 get 'listings/allsales/:id', to: 'listings#all_sales', as: "listings_all_sales"
 get 'listings/allsessions/:id', to: 'listings#all_sessions', as: "listings_all_sessions"



  #ROOT IS LANDING BUT TO BE CHANGED ONCE WE LAUNCH
  root 'landing_page#index'

  #path for landing page

  get '/', to: 'landing_page#index' , as: "landing_page"

#paths for job spec pdfs - controller has click tracking so we can track download numbers
get 'landing_page/download/SEPDF', to: 'landing_page#downloadSEPDF', as: "downloadSEPDF"
get 'landing_page/download/DMPDF', to: 'landing_page#downloadDMPDF', as: "downloadDMPDF"
get 'landing_page/download/SEINTERN', to: 'landing_page#downloadSEInternPDF', as: "downloadSEInternPDF"
get 'landing_page/download/DMINTERN', to: 'landing_page#downloadDMInternPDF', as: "downloadDMInternPDF"

post '/welcome', to: 'welcome#setUserLocation', as: "ajax_Set_User_Location"


   #add route for internal metrics page

   get '/metrics', to: 'metrics#index', as: 'internal_metrics'

   #add route for customer management system

   get 'cms', to: 'customer_management_system#index', as: 'customer_management_system'


 end

根据EXAMPLE.COM的建议更新 为example.com执行了一个grep - 结果如下:

http://pastebin.com/WgcHcRAg

正如你所看到的 - 源代码树中没有任何内容(除了一些与此问题无关的邮件浏览器内容) - 但很多提及tmp和capybara ......所以这个example.com来自哪里?在我看来,它来自于某处的水豚配置......

2 个答案:

答案 0 :(得分:4)

不要在访问时使用url帮助程序,而是使用路径帮助程序。

compile 'com.android.support:appcompat-v7:22.1.1'
compile 'com.astuetz:pagerslidingtabstrip:1.0.1'
compile 'com.google.android.gms:play-services:7.5.0'
compile files('libs/commons-io-2.4.jar') 

使用url帮助程序时,rails会添加在default_url_options设置中指定的任何主机和端口。这些测试没有js:true,因为racktest驱动程序忽略了主机名

答案 1 :(得分:0)

将JS与Capybara Webkit一起使用时,我建议使用数据库清理程序并将事务处理程序设置为false。

修改

规格/ rails_helper.rb

config.use_transactional_fixtures = false

您的应用程序未在测试数据库中看到您的用户,因为它包含在事务夹具中。

Per Capybara README ......

  

由于事务通常不是跨线程共享的,因此这会导致您在测试代码中放入数据库的数据对Capybara不可见。

您可以在Rspec / Capybara部分的database_cleaner README中找到更多信息。