Rails请求规范-ArgumentError:至少需要2个参数

问题描述 投票:0回答:1

我正在制定第一个请求规范。我有以下设置。

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.login, 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                  { "[email protected]" }
  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和hash作为2个参数(位于provider_dashboard_spec.rb,第一个规范中)。我希望#post使用参数作为身份验证并返回401来访问URI。

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

我正在制定第一个请求规范。我有以下设置。 provider_dashboard_spec.rb需要“ rails_helper”需要“ spec_helper”描述“ ProviderDashboard”,:type =>:request do ...

ruby-on-rails-5 rspec-rails
1个回答
0
投票
  • 您可能会看到ArgumentError,因为它需要请求中paramsheaders的参数,但它只接收params。如果这样不能解决问题,请使用完整的错误消息更新您的帖子。
© www.soinside.com 2019 - 2024. All rights reserved.