Ruby on rails 如何";浏览「;具有minitest集成测试的站点

Ruby on rails 如何";浏览「;具有minitest集成测试的站点,ruby-on-rails,integration-testing,minitest,Ruby On Rails,Integration Testing,Minitest,我正在尝试在rails上编写一个集成测试,该测试应该访问我的各个页面,但我无法通过登录 require 'test_helper' class UserSimulationTest < ActionDispatch::IntegrationTest test "login site" do # login via https https! get "users/sign_in" assert_response :success p

我正在尝试在rails上编写一个集成测试,该测试应该访问我的各个页面,但我无法通过登录

    require 'test_helper'

class UserSimulationTest < ActionDispatch::IntegrationTest


  test "login site" do
    # login via https
    https!
    get "users/sign_in"
    assert_response :success

    post_via_redirect "users/sign_in", username: users(:User_1).email
    assert_equal "/users/sign_in", path

    https?
    assert_response :success
  end
  test "go to voting" do
    https!
    get "voting"
    post_via_redirect "users/sign_in", username: users(:User_1).email
    post_via_redirect "voting"
    assert_equal "/voting", path
    assert_response :success
  end
end
需要“测试助手”
类UserSimulationTest
然后我得到了这个错误,因为它再次将我重定向到登录

Minitest::Assertion: Expected: "/voting"
  Actual: "/users/sign_in"
test/integration/user_simulation_test.rb:23:in `block in <class:UserSimulationTest>'
Finished in 0.84105s
2 tests, 4 assertions, 1 failures, 0 errors, 0 skips

Process finished with exit code 0
Minitest::断言:应为:“/voting”
实际:“/用户/登录”
测试/集成/用户模拟测试.rb:23:in'block in'
完成时间为0.84105s
2次测试,4次断言,1次失败,0次错误,0次跳过
进程已完成,退出代码为0

我通过从设备中传递正确的用户名和密码参数,成功地解决了问题。我正在使用Desive,所以我在test_helper.rb中创建了一个登录方法

def login(user)
    post_via_redirect user_session_path, 'user[email]' => user.email, 'user[password]' => user.encrypted_password
  end

除非您已登录,否则您的应用程序似乎已设置为重定向。这意味着对于每个测试,您都应该登录。因此,提取登录到测试助手所需的操作。然后,您可以在需要登录的每个测试中调用该方法。但在进入投票页面之前,我已经尝试登录了。它仍然重定向我。我将我的登录方法放在测试助手中,但我再次被重定向。看起来没什么区别。你编辑了你的问题吗?你想要登录,然后去投票。您似乎要进行投票,然后登录,然后发布到投票。似乎您的登录步骤错误,用户未登录。重新检查登录的post请求中的参数-通常您必须使用标准参数架构并添加模型名称,如
user:{username:'user-1-name'}