2012-04-02 165 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