FactoryGirl ArgumentError:参数个数错误(给定1,预期为0)

时间:2017-09-17 15:06:38

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

我正在为铁路运行测试'控制器和我发现了一些奇怪的行为。在另一个控制器的rspec测试文件中,来自FactoryGirl(构建,创建)的两个方法都按预期工作,这个新的测试文件引发了ArgumentError:每当我调用create方法时,参数数量错误(给定1,预期为0)

这是文件的样子:

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

我正在使用设计btw

这就是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

1 个答案:

答案 0 :(得分:0)

您似乎已用

覆盖了create
subject(:create) 

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

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

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