ruby-on-rails - Minitest 和 Fixtures:传递正确的参数?
问题描述
所以我试图在 Rails 中创建一个简单的集成测试。我想测试我的登录表单。我只是无法真正弄清楚如何正确传递我的参数。我的 .yml 文件称为 agent.yml。我的夹具文件是:
one:
first_name: firstNameTest
last_name: lastNameTest
email: test@test.com
encrypted_password: <%= Devise::Encryptor.digest(Agent, '123456') %>
那应该没问题。
我试过的两个测试都给了我一个错误。第一个,遵循 Ruby.docs:
class FlowsTest < ActionDispatch::IntegrationTest
test "Login and navigate" do
get "/agents/sign_in"
post "/agents/sign_in", email: agents(:one).email, password:
agents(:one).password
follow_redirect!
end
end
第二个版本:
class FlowsTest < ActionDispatch::IntegrationTest
test "Login and navigate" do
get "/agents/sign_in"
post "/agents/sign_in", agent: {email: agents(:one).email, password: '123456'}
follow_redirect!
end
end
两者都给我一个错误:
Error:
FlowsTest#test_Login_and_navigate:
ArgumentError: unknown keywords: email, password
test/integration/Flows_test.rb:6:in `block in <class:FlowsTest>'
我想我以错误的方式传递参数。因为电子邮件和密码应该从固定装置中获取,还是我错了?任何人都可以帮忙吗?将不胜感激。谢谢大家!
解决方案
解决者:
require 'test_helper'
class FlowsTest < ActionDispatch::IntegrationTest
fixtures :agents
def test_login
get "/agents/sign_in"
assert_equal 200, status
post "/agents/sign_in", params: { email: agents(:one).email,
password: agents(:one).password }
follow_redirect!
assert_equal 200, status
assert_equal "/", path
end
end
推荐阅读
- javascript - 不同表单的不同表单提交操作
- dart - Dart:存储货币的最佳类型
- php - 如何在php中获取年、月、周、周中的日期
- python-3.x - 这个错误“不是在字符串格式化期间转换的所有参数”是什么意思?
- verilog - 如何为多个模块编写测试平台?
- java - JavaFX JAR 工件添加媒体导致主类错误
- r - 如何在R中重新构造矩阵?
- javascript - Django响应将json设置为cookie
- java - 如何在Nodejs(如Java)中将字符串转换为字节数组
- python - 如何反转标签的 One-Hot Encoding 以评估 ML/DL 模型?