在Rails 3.1中使用Capybara,Rspec和Selenium进行测试时登录失败

JHo*_*JHo 16 selenium capybara rspec-rails ruby-on-rails-3 factory-bot

我为我的Rails 3.1应用程序添加了一些确认对话框,在此之前,他们进行了相应的测试.按照Railscast#257的模型,我在测试中添加了':js => true',添加了database_cleaner并相应地修改了spec_helper.rb文件.

当我运行测试时,Firefox启动,Capybara-Selenium在字段中填入相应的用户名和密码,但登录失败(即"无效的用户名/密码".)其他没有':js =的测试> true'并且还登录,仍然通过.

我想在将来为我的应用程序添加更多javascript,并且我正在避免破解Capybara以使其工作的解决方案(例如,在所有对话框上单击"确定".)

我可能会缺少什么想法?失败,有关如何调试此问题的任何建议?

谢谢.

Jen*_*ich 14

你应该为你的seleniumtests设置use_transactional_fixtures = false.这可以在spec_helper.rb中完成

config.use_transactional_fixtures = false 
Run Code Online (Sandbox Code Playgroud)

对于你所有的测试.

或者为单个测试用例执行此操作:

describe 'testcase' , :type => :request do
  self.use_transactional_fixtures = false
  it 'your test', :js => :true do
    testing...
  end
end
Run Code Online (Sandbox Code Playgroud)

发生这种情况是因为selenium-tests以不同的方式访问数据库.启用事务夹具后,selenium将在空数据库上工作 - >您的用户不存在而您无法登录.

对于正常测试,您应该使用事务性夹具,因为您的测试运行得更快.


man*_*ire 9

根据Avdi Grimm的帖子(详细说明):

的Gemfile:

group :test do
  gem 'database_cleaner'
end
Run Code Online (Sandbox Code Playgroud)

spec_helper.rb:

config.use_transactional_fixtures = false 
Run Code Online (Sandbox Code Playgroud)

规格/支持/ database_cleaner.rb:

RSpec.configure do |config|

  config.before(:suite) do
    DatabaseCleaner.clean_with(:truncation)
  end

  config.before(:each) do
    DatabaseCleaner.strategy = :transaction
  end

  config.before(:each, :js => true) do
    DatabaseCleaner.strategy = :truncation
  end

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

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

end
Run Code Online (Sandbox Code Playgroud)

我禁用了事务夹具,但添加了:js => truedatabase_cleaner.rb为我做了它.