2010-11-27 44 views
6

我想写一个轨道集成测试(使用ActionDispatch::IntegrationTest)。我正在使用设计进行认证和机械师测试模型。我无法成功登录使用设计宝石的Rails集成测试

下面是一个简单的例子:

class UserFlowsTest < ActionDispatch::IntegrationTest 
    setup do 
    User.make 
    end 
    test "sign in to the site" 
    # sign in 
    post_via_redirect 'users/sign_in', :email => '[email protected]', :password => 'qwerty'  
    p flash 
    p User.all 
end 

下面是调试输出:

Loaded suite test/integration/user_flows_test 
Started 
{:alert=>"Invalid email or password."} 
[#<User id: 980190962, email: "", encrypted_password: "", password_salt: "", reset_password_token: nil, remember_token: nil, remember_created_at: nil, sign_in_count: 0, current_sign_in_at: nil, last_sign_in_at: nil, current_sign_in_ip: nil, last_sign_in_ip: nil, created_at: "2010-11-27 16:44:10", updated_at: "2010-11-27 16:44:10">, #<User id: 980190963, email: "[email protected]", encrypted_password: "$2a$10$vYSpjIfAd.Irl6eFvhJL0uAwp4qniv5gVl4O.Hnw/BUR...", password_salt: "$2a$10$vYSpjIfAd.Irl6eFvhJL0u", reset_password_token: nil, remember_token: nil, remember_created_at: nil, sign_in_count: 0, current_sign_in_at: nil, last_sign_in_at: nil, current_sign_in_ip: nil, last_sign_in_ip: nil, created_at: "2010-11-27 17:09:13", updated_at: "2010-11-27 17:09:13">] 
"/unauthenticated" 
F 

这里是我的blueprints.rb:

require 'machinist/active_record' 
require 'sham' 

User.blueprint do 
    email {"[email protected]"} 
    password {"qwerty"} 
end 
+0

我最近开始阅读Rails Test Prescriptions(http://pragprog.com/titles/nrtest/rails-test-prescriptions)。在书中相当早的时候,Noel提到他将使用Devise进行认证。我跳到github.com看他的示例应用程序(https://github.com/noelrappin/huddle),看看他是否能解决这个问题。虽然他没有任何集成测试功能,但我将其发布在此处供参考。 – hiwaylon 2010-11-30 12:40:09

+0

我也切换(返回)在我的应用程序中使用AuthLogic(https://github.com/binarylogic/authlogic)进行身份验证。我之前使用过它,喜欢它比Devise好得多。 – hiwaylon 2010-11-30 12:41:37

回答

2

我在Devise的书中没有样例集成测试,但我认为Cucumbe的步骤定义中的代码r章也将工作,如果你有Webrat /水豚安装:

@user = User.create!(:email => "[email protected]", 
      :password => "password", 
      :password_confirmation => "password") 
visit "login" 
fill_in("user_email", :with => @user.email) 
fill_in("user_password", :with => "password") 
click_button("Sign In") 
+0

感谢您的回复。我将不得不再次挖掘这个项目,看看它是如何工作的。 +1现在。另外,感谢您的好阅读:) – hiwaylon 2011-08-17 21:39:54

8

首先,与设计,你可能需要“确认”用户。

你可以做这样的事情:

user = User.make! 
user.confirmed_at = Time.now 
user.save! 

这里是没有机械师一个例子(但你有以上的部分替换用户创建代码部分):

到test_integration_helper:

require "test_helper" 
require "capybara/rails" 

    module ActionController 
     class IntegrationTest 
     include Capybara 

     def sign_in_as(user, password) 
      user = User.create(:password => password, :password_confirmation => password, :email => user) 
      user.confirmed_at = Time.now 
      user.save! 
      visit '/' 
      click_link_or_button('Log in') 
      fill_in 'Email', :with => user.email 
      fill_in 'Password', :with => password 
      click_link_or_button('Sign in') 
      user  
     end 
     def sign_out 
      click_link_or_button('Log Out') 
     end 
     end 
    end 

和你的integration_test:

require 'test_integration_helper' 

class UsersIntegrationTest < ActionController::IntegrationTest 

    test "sign in and then sign out" do 
    #this helper method is into the test_integration_helper file     
    sign_in_as("[email protected]", "monkey")   
    assert page.has_content?('Signed in successfully'), "Signed in successfully" 

    sign_out   
    assert page.has_content?('Signed out successfully'), "Signed out successfully" 
    end 
end 
+0

感谢您的回应。我将不得不再次挖掘这个项目,看看它是如何工作的。 +1现在。 – hiwaylon 2011-08-17 21:39:20

1

如果任何人有类似的问题...

我是在一个类似的情况(从Authlogic迁移),并有此相同的问题。问题出现在devise.rb初始化程序中。默认情况下,它将您的测试环境设置为在密码的加密/解密期间仅使用1次延伸。我真的不知道什么是拉伸,但对于设计使用旧的Authlogic加密密码,延伸必须是20.因为我试图登录一个用户,在我的测试中,他的密码最初由Authlogic加密,设计也需要在测试中使用20个拉伸。我改变了配置如下图所示:

# Limiting the stretches to just one in testing will increase the performance of 
# your test suite dramatically. However, it is STRONGLY RECOMMENDED to not use 
# a value less than 10 in other environments. 
- config.stretches = Rails.env.test? ? 1 : 20 
+ config.stretches = 20 
10

它不工作的原因是捏造,

'user[email]' 
'user[password]' 

创建表单字段名称和post_via_redirect预计这些名称作为参数。因此,以下声明将成功登录。

post_via_redirect 'users/sign_in', 'user[email]' => '[email protected]', 'user[password]' => 'qwerty'