首页 > 解决方案 > 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>'

我想我以错误的方式传递参数。因为电子邮件和密码应该从固定装置中获取,还是我错了?任何人都可以帮忙吗?将不胜感激。谢谢大家!

标签: ruby-on-railstestingminitest

解决方案


解决者:

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

推荐阅读