ruby-on-rails – Factory Girl / Capybara从数据库中试中删除记录?

前端之家收集整理的这篇文章主要介绍了ruby-on-rails – Factory Girl / Capybara从数据库中试中删除记录?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
使用RSpec& Capybara,我正在获得一个有趣的测试失败模式,在测试用例中删除了一些细微的重新排列的事情…这些东西,不要紧.

我正在开发自己的身份验证系统.它正在工作,我可以登录/退出浏览器和会话工作等等.但是,尝试测试这是失败的.有些事情正在发生,我不太明白,这似乎取决于(看似)无关的电话的顺序.

  1. require 'spec_helper'
  2.  
  3. describe "Sessions" do
  4. it 'allows user to login' do
  5. #line one
  6. user = Factory(:user)
  7. #For SO,this method hashes the input password and saves the record
  8. user.password! '2468'
  9.  
  10. #line two
  11. visit '/sessions/index'
  12.  
  13.  
  14. fill_in 'Email',:with => user.email
  15. fill_in 'Password',:with => '2468'
  16. click_button 'Sign in'
  17.  
  18. page.should have_content('Logged in')
  19. end
  20. end

就是这样,测试失败…登录失败.在将“调试器”调用插入规范和控制器之后,我可以看到为什么:就控制器而言,用户没有被插入到数据库中:

编辑在ApplicationController中添加

  1. class ApplicationController < ActionController::Base
  2. helper :all
  3. protect_from_forgery
  4.  
  5. helper_method :user_signed_in?,:guest_user?,:current_user
  6.  
  7. def user_signed_in?
  8. !(session[:user_id].nil? || current_user.new_record?)
  9. end
  10.  
  11. def guest_user?
  12. current_user.new_record?
  13. end
  14.  
  15. def current_user
  16. @current_user ||= session[:user_id].nil? ? User.new : User.find(session[:user_id])
  17. rescue ActiveRecord::RecordNotFound
  18. @current_user = User.new
  19. flash[:notice] = 'You\'ve been logged out.'
  20. end
  21. end
  22.  
  23.  
  24. class SessionsController < ApplicationController
  25. def login
  26. user = User.where(:email=>params[:user][:email]).first
  27.  
  28. debugger ###
  29.  
  30. if !user.nil? && user.valid_password?(params[:user][:password])
  31. #engage session
  32. else
  33. #run away
  34. end
  35. end
  36.  
  37. def logout
  38. reset_session
  39. redirect_to root_path,:notice => 'Logget Out.'
  40. end
  41. end

在控制台中,在上述断点处:

  1. 1.9.2 vox@Alpha:~/Sites/website$rspec spec/controllers/sessions_controller_spec.rb
  2. /Users/vox/Sites/website/app/controllers/sessions_controller.rb:7
  3. if !user.nil? && user.valid_password?(params[:user][:password])
  4. (rdb:1) irb
  5. ruby-1.9.2-p180 :001 > User.all.count
  6. => 0
  7. ruby-1.9.2-p180 :002 >

但是,如果我在我的测试中重新排列了几行,那么把“二”放在“一”之上:

  1. describe "Sessions" do
  2. it 'allows user to login' do
  3. #line two
  4. visit '/sessions/index'
  5.  
  6. #line one
  7. user = Factory(:user)
  8. #For SO,this method hashes the input password and saves the record
  9. user.password! '2468'
  10.  
  11.  
  12. fill_in 'Email',:with => '2468'
  13. click_button 'Sign in'
  14.  
  15. page.should have_content('Logged in')
  16. end
  17. end

我在控制台中得到这个(与上述相同的断点):

  1. 1.9.2 vox@Alpha:~/Sites/website$rspec spec/controllers/sessions_controller_spec.rb
  2. /Users/vox/Sites/website/app/controllers/sessions_controller.rb:7
  3. if !user.nil? && user.valid_password?(params[:user][:password])
  4. (rdb:1) irb
  5. ruby-1.9.2-p180 :001 > User.all.count
  6. => 1

为了简洁起见,我省略了用户对象内容的全部转储,但是我可以向您保证测试按预期完成.

这种交换线路来测试通过的行为与我对这些命令应该发生什么的想法并不十分吻合,并且证明对我在其他领域的测试是相当痛苦的.

关于这里发生了什么的提示

为了解决这个问题,我已经对google和SO进行了研究,而且还没有关于RSpec / Capybara和Sessions的SO问题.没有什么似乎很适合.

谢谢你的看

更新

添加了一个断点(就在访问调用之前)和一些调试测试,并回到这里:

  1. (rdb:1) user
  2. #<User id: 1,login_count: 1,email: "testuser1@website.com",encrypted_password: "11f40764d011926eccd5a102c532a2b469d8e71249f3c6e2f8b...",salt: "1313613794">
  3. (rdb:1) User.all
  4. [#<User id: 1,salt: "1313613794">]
  5. (rdb:1) next
  6. /Users/vox/Sites/website/spec/controllers/sessions_controller_spec.rb:19
  7. fill_in 'Email',:with => user.email
  8. (rdb:1) User.all
  9. []

那么清楚这个访问方式的一些事情是告诉工厂女孩,它用用户对象完成,所以她删除它?

编辑仔细检查test.log后,没有任何内容发出任何删除.所以我或多或少地回到广场.

解决方法

在工厂女孩邮件列表的帮助下,我发现了这个问题.

默认情况下,RSpec使用事务来保持数据库处于干净状态,每个事务都绑定到线程.沿着管道的某个地方,visit_page命令分解,绑定到当前线程的事务会消失.

解决方案很简单:禁用事务.

  1. describe "Sessions" do
  2. self.use_transactional_fixtures = false
  3.  
  4. it 'no longer uses transactions' do
  5. #whatever you want
  6. end
  7. end

更新Rails 5.1

从Rails 5.1起,use_transactional_fixtures已被弃用,应替换为use_transactional_tests.

  1. self.use_transactional_tests = false

猜你在找的Ruby相关文章