【问题标题】:FactoryGirl ArgumentError: wrong number of arguments (given 1, expected 0)FactoryGirl ArgumentError:参数数量错误(给定 1,预期为 0)
【发布时间】:2017-09-18 16:54:04
【问题描述】:

我正在为 rails 的控制器运行测试,但我发现了一些奇怪的行为。虽然在另一个控制器的 rspec 测试文件中,FactoryGirl (build, create) 的两种方法都按预期工作,但这个新的测试文件会引发 ArgumentError: wrong number of arguments (given 1, expected 0) 每当我调用 create 方法时.

文件是这样的:

describe DoctorsController, type: :controller do                                                   
  let(:user) { create(:user) }                                         

顺便说一句,我正在使用设计

这是 FactoryGirl 定义的样子:

require 'faker'

FactoryGirl.define do
  factory :user do
    email { Faker::Internet.email }
    password "password"
    password_confirmation "password"
  end
end

不同控制器中的同一行就像一个魅力:

describe ClinicsController, type: :controller do

  context 'when signed in' do
    let(:clinic) { build(:clinic) }
    let(:user) { create(:user) }
    before(:each) { sign_in user }

知道为什么会这样吗? 提前致谢

完整示例:

require 'rails_helper'

describe DoctorsController, type: :controller do
  let(:doctor) { build(:doctor) }
  let(:params) { doctor.attributes }
  let(:user) { create(:user) }

  it 'should be an instance of a secure controller' do
    expect(DoctorsController.new).to be_a(SecureApplicationController)
  end

  describe '#create' do
    subject(:create) { post :create, doctor: params }
    context 'when signed in' do
      before(:each) { sign_in user }

      context 'on success' do
        it 'should redirect with notification' do
          expect(create).to redirect_to(doctors_path)
          expect(flash[:notice]).to eq('Doctor creado satisfactoriamente')
        end
        it 'should create a new doctor' do
          expect{ create }.to change { Doctor.count }.by(1)
        end
        it 'should assign existing clinics when creating a doctor' do
          clinics = [create(:clinic), create(:clinic)]
          post :create, doctor: params, clinics: clinics.map(&:id)
        end
      end

      context 'on failure' do
        subject(:create) { post :create,
                           doctor: attributes_for(:doctor, name: nil) }
        it 'should redirect with notification' do
          expect(create).to redirect_to(new_doctor_path)
          expect(flash[:notice]).to eq('Error creando el doctor')
        end
      end
    end
  end
end

堆栈跟踪:

Failures:

  1) DoctorsController#create when signed in on success should redirect with notification
     Failure/Error: let(:user) { create(:user) }

     ArgumentError:
       wrong number of arguments (given 1, expected 0)
     # ./spec/controllers/doctors_controller_spec.rb:6:in `block (2 levels) in <top (required)>'
     # ./spec/controllers/doctors_controller_spec.rb:17:in `block (4 levels) in <top (required)>'

  2) DoctorsController#create when signed in on success should create a new doctor
     Failure/Error: let(:user) { create(:user) }

     ArgumentError:
       wrong number of arguments (given 1, expected 0)
     # ./spec/controllers/doctors_controller_spec.rb:6:in `block (2 levels) in <top (required)>'
     # ./spec/controllers/doctors_controller_spec.rb:17:in `block (4 levels) in <top (required)>'

  3) DoctorsController#create when signed in on success should assign existing clinics when creating a doctor
     Failure/Error: let(:user) { create(:user) }

     ArgumentError:
       wrong number of arguments (given 1, expected 0)
     # ./spec/controllers/doctors_controller_spec.rb:6:in `block (2 levels) in <top (required)>'
     # ./spec/controllers/doctors_controller_spec.rb:17:in `block (4 levels) in <top (required)>'

  4) DoctorsController#create when signed in on failure should redirect with notification
     Failure/Error: let(:user) { create(:user) }

     ArgumentError:
       wrong number of arguments (given 1, expected 0)
     # ./spec/controllers/doctors_controller_spec.rb:6:in `block (2 levels) in <top (required)>'
     # ./spec/controllers/doctors_controller_spec.rb:17:in `block (4 levels) in <top (required)>'

Finished in 0.0092 seconds (files took 5.95 seconds to load)
5 examples, 4 failures

【问题讨论】:

  • 你能展示失败的例子和完整的错误吗?
  • 试试let(:user) { FactoryGirl.create(:user) }
  • @Babar 已经这样做了 :(
  • 还有什么作用?另外 - 请提供ClinicsController 的完整工作规格 - 比较容易。

标签: ruby-on-rails rspec factory-bot


【解决方案1】:

看起来你已经用

覆盖了create
subject(:create) 

它不再指向FactoryGirl.create,现在创建评估不期望任何参数的主题。

您可以使用不同的名称定义主题,甚至可以不使用任何名称并执行

   subject { post :create ...} 
   expect(subject).to ...

【讨论】:

  • 感谢@Babar 和meta,似乎是主题被覆盖和一些奇怪的文件系统行为的组合。我删除了工厂,重命名了主题,现在可以正常工作了。再次感谢!
猜你喜欢
  • 2023-01-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多