gpt4 book ai didi

ruby-on-rails - 使用devise gem进行Rails集成测试

转载 作者:行者123 更新时间:2023-12-04 09:28:51 25 4
gpt4 key购买 nike

我想编写一个Rails集成测试(使用ActionDispatch::IntegrationTest)。我正在使用devise进行身份验证,并使用机械师进行测试模型。我无法成功登录。

这是一个简单的示例:

class UserFlowsTest < ActionDispatch::IntegrationTest
setup do
User.make
end
test "sign in to the site"
# sign in
post_via_redirect 'users/sign_in', :email => 'foo@bar.com', :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: "foo@bar.com", 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 {"foo@bar.com"}
password {"qwerty"}
end

最佳答案

它不起作用的原因是设计将表单字段名称创建为

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

而post_via_redirect则希望这些名称作为参数。因此,以下语句将使登录成功。
post_via_redirect 'users/sign_in', 'user[email]' => 'foo@bar.com', 'user[password]' => 'qwerty' 

关于ruby-on-rails - 使用devise gem进行Rails集成测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4292872/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com