Rails request specs - ArgumentError: at least 2 arguments required

Rails request specs - ArgumentError: at least 2 arguments required

我正在处理我的第一个请求规范。我有以下设置。

provider_dashboard_spec.rb

require "rails_helper"
require "spec_helper"

describe "ProviderDashboard" , :type => :request do

  let!(:user) { Fabricate(:user) }
  let!(:invalid_user) { Fabricate(:invalid_user) }

  context "when failure" do
    # 401: Unauthorized - Authentication credentials were invalid.
    # 403: Forbidden - The resource requested is not accessible - in a Rails app, this would generally be based on permissions.
    # 404: Not Found - The resource doesn’t exist on the server.

    it "returns status: 401 Unauthorized with invalid or missing credentials" do
      post "api/v1/sessions", :params => {login: invalid_user.email, passowrd: invalid_user.password}
      # expect(response.code).to be('401')
      # expect(respone.body).to include('error: ')
    end
  end
end

user_fabricator.rb

require 'faker'
require 'ffaker'
Fabricator(:user, from: User) do
  # user
  first_name            { FFaker::Name.first_name }
  last_name             { FFaker::Name.last_name }
  dob                   { FFaker::Time.date }
  gender                { 'Male' }
  email                 Fabricate.sequence(:email) { |i| "test_#{i}@example.com" }
  password              { '123456' }
  password_digest       { '123456' }
  password_confirmation { '123456' }
  slug                  { "#{FFaker::Name.first_name} + #{FFaker::Name.last_name}" }
end

Fabricator(:invalid_user, from: User) do
  first_name             { FFaker::Name.first_name }
  last_name              { FFaker::Name.last_name }
  dob                    { '' }
  email                  { "invalid_test@example.com" }
  password               { '' }
  password_digest        { '' }
  password_confirmation  { '' }
end

spec_helper.rb

require 'capybara/rspec'
require 'pathname'
require 'request_helper'
.
.
.
RSpec.configure do |config|
  config.include Sorcery::TestHelpers::Rails::Controller, type: :controller
  config.include Sorcery::TestHelpers::Rails::Integration, type: :feature

  config.expect_with :rspec do |expectations|
    expectations.include_chain_clauses_in_custom_matcher_descriptions = true
  end
  config.mock_with :rspec do |mocks|
    mocks.verify_partial_doubles = true
  end
end

rails_helper.rb

ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../config/environment', __dir__)
# Prevent database truncation if the environment is production
abort('The Rails environment is running in production mode!') if Rails.env.production?
require 'spec_helper'
require 'rspec/rails'
require 'shoulda/matchers'
require 'capybara/rails'
require 'ffaker'

RSpec.configure do |config|
  # Fabrication::Support.find_definitions
  config.include Sorcery::TestHelpers::Rails::Controller
  config.include Sorcery::TestHelpers::Rails::Integration
  config.include Rails.application.routes.url_helpers
  config.filter_rails_from_backtrace!

  Shoulda::Matchers.configure do |config|
    config.integrate do |with|
      with.test_framework :rspec
      with.library :rails
    end
  end

  config.before(:suite) do
    DatabaseCleaner.strategy = :transaction
    DatabaseCleaner.clean_with(:truncation)
  end

  config.around(:each) do |example|
    DatabaseCleaner.cleaning do
      example.run
    end
  end
end

我无法理解为什么#post 不接受我的 url 和散列作为 2 个参数(位于 provider_dashboard_spec.rb,第一个规范)。我希望#post 访问 URI,使用参数作为身份验证并返回 401。

相反,我收到了 ArgumentError:至少需要 2 个参数。我没有在我的规范或 rails 助手中包含正确的库吗?

更新#1

我已经更改规范以包含 header 所推荐的内容。

it "returns status: 401 Unauthorized with invalid or missing credentials" do
  params = {login: invalid_user.email, password: invalid_user.password}
  header = {"CONTENT_TYPE" => "text/json"}
  post "/api/v1/sessions", params, header
end

我的错误现在在 params 赋值时触发,但是是同样的错误。

  1) ProvidersController when failure returns status: 401 Unauthorized with invalid or missing credentials
     Failure/Error: params = {login: invalid_user.login, password: invalid_user.password}

     ArgumentError:
       at least 2 arguments required
     # /home/barracus/.rvm/gems/ruby-2.3.8/gems/sorcery-0.9.1/lib/sorcery/model.rb:85:in `authenticate'
     # /home/barracus/.rvm/gems/ruby-2.3.8/gems/sorcery-0.9.1/lib/sorcery/controller.rb:33:in `login'
     # ./spec/requests/api/v1/providers/provider_dashboard_spec.rb:16:in `block (3 levels) in <top (required)>'
     # ./spec/rails_helper.rb:79:in `block (3 levels) in <top (required)>'
     # /home/barracus/.rvm/gems/ruby-2.3.8/gems/database_cleaner-1.7.0/lib/database_cleaner/generic/base.rb:16:in `cleaning'
     # /home/barracus/.rvm/gems/ruby-2.3.8/gems/database_cleaner-1.7.0/lib/database_cleaner/base.rb:100:in `cleaning'
     # /home/barracus/.rvm/gems/ruby-2.3.8/gems/database_cleaner-1.7.0/lib/database_cleaner/configuration.rb:86:in `block (2 levels) in cleaning'
     # /home/barracus/.rvm/gems/ruby-2.3.8/gems/database_cleaner-1.7.0/lib/database_cleaner/configuration.rb:87:in `cleaning'
     # ./spec/rails_helper.rb:78:in `block (2 levels) in <top (required)>'

Finished in 1.14 seconds (files took 1.65 seconds to load)
1 example, 1 failure

最终更新

原来我有另一个错字 invalid_user.login 而不是 invalid_user.email 并且必须为参数、headers、env 和 xhr 格式提供关键字参数。

感谢协助!

其他来源:

  • 您可能会看到一个 ArgumentError,因为它需要请求中的 paramsheaders 参数,但它只接收到 params。如果这不能解决问题,请使用完整的错误消息更新您的 post。
  • 您在 POST 请求中将密码拼错为 passowrd
  • 建议:将注释掉的状态代码示例替换为 HTTP status matcher 个示例