2012-04-02 157 views
4

使用Javascript時無法獲得請求規格。 我的規格通過如果我沒有使用Javascript運行它們(該頁面被構建爲可以使用或不使用JS)。RSpec +水豚請求規格w/JS不能正常工作

具體來說,當我做斷言如Post.should have(1).record時,規格失敗。 水豚只是不從數據庫拿起記錄,並且數據庫不會在運行之間清理。

我試過使用DatabaseCleaner禁用事務夾具 - 我想這是常見的方法。沒有骰子。

我也試過(並且理想情況下更喜歡)沒有DatabaseCleaner的情況下運行,使用事務夾具並強制AR共享線程之間的相同連接(a patch described by José Valim)。再一次,沒有骰子。

另外,我也嘗試在Capybara-webkit和Selenium之間切換 - 問題依然存在。

我已經忍了只是一個基本的郵政支架一個示例應用程序,可複製的問題:https://github.com/cabgfx/js-specs 有事務性燈具一spec_helper.rb和AR共享連接,併爲其他場景中spec_helper_database_cleaner.rb。

我通常使用Spork,但我在兩個spec_helper.rb文件中禁用了它,只是爲了消除潛在的故障點(在這兩個應用程序中;「真實」的一個和示例應用程序)。

我在Macbook Air上使用Pow進行本地開發,運行帶有MRI 1.9.3到RVM的OS X 10.7.3。 (我也在1.9.2上試過)。

希望我是有道理的 - 任何指導/幫助/指針大大讚賞!

+0

在某一點這方面的工作? – 2012-04-03 07:15:23

+0

不使用Javascript。當我在規格中刪除js:true時,它仍然有效,並且仍然如此。 – cabgfx 2012-04-03 10:37:17

回答

3

馬特 - 非常感謝您抽出時間來幫助我! 我試着用你的spec_helper設置它,使用Selenium作爲javascript驅動。

該規範仍然失敗 - 但我可以看到在Firefox中執行的正確行爲... 然後,我明白,這個問題可能會發生,因爲水豚不等待AJAX​​請求完成。

然後,我恢復到我最初的spec_helper(與Spork和沒有DatabaseCleaner),並簡單地使用Capybara的wait_until { page.has_content? "text I'm inserting with JS" }

我更新了示例應用程序,並且在請求規範中只添加了sleep 1,這樣您就可以親眼看到。它現在可以和Spork一起使用,而且AR猴子補丁似乎可以很好地工作。

+1

wait_until已被棄用爲水豚2.0 – 2013-02-04 00:14:14

+0

啊,謝謝。很高興知道! – cabgfx 2013-02-04 12:56:03

+0

我相信你可以使用'find'方法,它自然會等到元素存在,如果超時,就會炸掉。消除了對「wait_until」的需求。 – Elliott 2013-08-27 16:35:48

1

我試過你的代碼,下面列出的spec_helper.rb和測試通過。請注意,觸發數據庫清理的語法與您的spec_helper_database_cleaner.rb有點不同。

我們在生產中使用它,我們也嘗試了Jose Valim提出的修改,但它對我們來說並不適用 - 這確實如此。

require 'rubygems' 
require 'spork' 
#uncomment the following line to use spork with the debugger 
#require 'spork/ext/ruby-debug' 

Spork.prefork do 
    # Loading more in this block will cause your tests to run faster. However, 
    # if you change any configuration or code from libraries loaded here, you'll 
    # need to restart spork for it take effect. 

    # This file is copied to spec/ when you run 'rails generate rspec:install' 
    ENV["RAILS_ENV"] ||= 'test' 
    require File.expand_path("../../config/environment", __FILE__) 
    require 'rspec/rails' 
    require 'rspec/autorun' 

    # Add this to load Capybara integration: 
    require 'capybara/rspec' 
    require 'capybara/rails' 

    include Capybara::DSL 

    # Requires supporting ruby files with custom matchers and macros, etc, 
    # in spec/support/ and its subdirectories. 
    Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f} 

    RSpec.configure do |config| 
    # ## Mock Framework 
    # 
    # If you prefer to use mocha, flexmock or RR, uncomment the appropriate line: 
    # 
    # config.mock_with :mocha 
    # config.mock_with :flexmock 
    # config.mock_with :rr 

    # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures 
    config.fixture_path = "#{::Rails.root}/spec/fixtures" 

    # If you're not using ActiveRecord, or you'd prefer not to run each of your 
    # examples within a transaction, remove the following line or assign false 
    # instead of true. 
    config.use_transactional_fixtures = false 

    # If true, the base class of anonymous controllers will be inferred 
    # automatically. This will be the default behavior in future versions of 
    # rspec-rails. 
    config.infer_base_class_for_anonymous_controllers = false 

    # Include sign_in & sign_out for tests 
    # config.include Devise::TestHelpers, :type => :controller 

    # Use database_cleaner to ensure a known good test db state as we can't use 
    # transactional fixures due to selenium testing 
    config.before(:suite) do 
     DatabaseCleaner.strategy = :truncation 
     DatabaseCleaner.clean_with(:truncation) 
    end 

    config.before(:each) do 
     DatabaseCleaner.start 
    end 

    config.after(:each) do 
     DatabaseCleaner.clean 
    end 
    end 
end 

Spork.each_run do 
    # This code will be run each time you run your specs. 
end 
0

何塞的建議爲我工作,但不是當我使用Spork。但是,增加這spec_helper.rb做:

Spork.prefork do 
    RSpec.configure do |config| 
    # Make it so poltergeist (out of thread) tests can work with transactional fixtures 
    # http://www.opinionatedprogrammer.com/2011/02/capybara-and-selenium-with-rspec-and-rails-3/#post-441060846 
    ActiveRecord::ConnectionAdapters::ConnectionPool.class_eval do 
     def current_connection_id 
     Thread.main.object_id 
     end 
    end 
    end 
end 

來源:http://www.opinionatedprogrammer.com/2011/02/capybara-and-selenium-with-rspec-and-rails-3/#post-441060846