【发布时间】: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