rspec,capybara javascript驱动程序

时间:2016-06-22 20:36:35

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

我的测试中有这个:

let!(:user) { create(:user) }

当我在没有:js

的情况下运行测试时
def create
   11:     byebug
=> 12:     if @user = login(params[:email], params[:password])
   13:       logger.info "--------------"
   14:       logger.info "found user"
   15:       redirect_back_or_to root_path, notice: t(:log_in_is_successful_notice)
   16:     else
(byebug) User.all
#<ActiveRecord::Relation [#<User id: 291, email: "test@test.com", crypted_password: "$2a$10$rOKscDDidaf93p/e9IOceOCGozns5AJ2zSLMO68jRZO...", created_at: "2016-06-22 20:15:55", updated_at: "2016-06-22 20:15:55", salt: "qpSR5US81thfoTn8pphQ", remember_me_token: nil, remember_me_token_expires_at: nil, reset_password_token: nil, reset_password_token_expires_at: nil, reset_password_email_sent_at: nil, current_block_id: nil, locale: "ru">]>

但是当我用:js运行此测试时,我得到了这个:

 11:     byebug
=> 12:     if @user = login(params[:email], params[:password])
   13:       logger.info "--------------"
   14:       logger.info "found user"
   15:       redirect_back_or_to root_path, notice: t(:log_in_is_successful_notice)
   16:     else
(byebug) User.all
#<ActiveRecord::Relation []>
(byebug) User.all.take
nil

我使用webkit作为js驱动程序:

Capybara.javascript_driver = :webkit

1 个答案:

答案 0 :(得分:1)

您尚未禁用事务测试,因此在您的测试线程中创建的对象在您的应用程序线程中不可见。您需要阅读Capybara README的Transactions and Database Setup部分,然后配置database_cleaner之类的内容来处理切换数据库策略