gpt4 book ai didi

ruby-on-rails - RSpec valid_attributes 在验证相关模型时添加什么

转载 作者:行者123 更新时间:2023-12-04 06:05:30 24 4
gpt4 key购买 nike

我是 RSpec 和 Cucumber BDD 开发的新手,这是一个很大的飞跃。

我使用 rake generate scaffold model_name 生成了大量代码,这些代码似乎有一定道理,但我对它们不是很有信心。

在我添加一些数据库关系和模型验证之前,我使用 RSpec 的表现还不错,然后一切都崩溃了,我真的不知道修复它的最佳实践或最干净的方法是什么。

很明显我的问题是“有效属性”需要用链接到有效患者/麻醉师/手术/外科医生的外键来定义,但我不知道我应该如何在 RSpec 中写这个(也许使用 FactoryGirl 来生成每个模型的有效对象?)模型 RSpec 代码对我来说很有意义,但 Controller 代码超出了我的理解范围。

似乎它应该在一行或四行(每个对象一行)中完成,但仅此而已。

代码

#app/models/procedure.rb
class Procedure < ActiveRecord::Base
belongs_to :patient
belongs_to :surgeon
belongs_to :anaesthetist
belongs_to :hospital

validates :patient_id, presence: true
validates :surgeon_id, presence: true
validates :anaesthetist_id, presence: true
validates :hospital_id, presence: true
end

#spec/models/procedure_spec.rb
require 'spec_helper'

describe Procedure do

it { should validate_presence_of(:patient_id) }
it { should validate_presence_of(:surgeon_id) }
it { should validate_presence_of(:anaesthetist_id) }
it { should validate_presence_of(:hospital_id) }

end

#spec/controllers/procedures_controller_spec.rb
describe ProceduresController do

# This should return the minimal set of attributes required to create a valid
# Procedure. As you add validations to Procedure, be sure to
# adjust the attributes here as well.
let(:valid_attributes) { { "description" => "MyText" } }

# This should return the minimal set of values that should be in the session
# in order to pass any filters (e.g. authentication) defined in
# ProceduresController. Be sure to keep this updated too.
let(:valid_session) { {} }

describe "GET index" do
it "assigns all procedures as @procedures" do
procedure = Procedure.create! valid_attributes
get :index, {}, valid_session
assigns(:procedures).should eq([procedure])
end
end

....

end

#Terminal output, note failures 4 and 5, there's about 10 more errors after that with the exact same cause
Failures:

1) ProceduresController POST create with valid params redirects to the created procedure
Failure/Error: response.should redirect_to(Procedure.last)
Expected response to be a <redirect>, but was <200>
# ./spec/controllers/procedures_controller_spec.rb:80:in `block (4 levels) in <top (required)>'

2) ProceduresController POST create with valid params assigns a newly created procedure as @procedure
Failure/Error: assigns(:procedure).should be_persisted
expected persisted? to return true, got false
# ./spec/controllers/procedures_controller_spec.rb:75:in `block (4 levels) in <top (required)>'

3) ProceduresController POST create with valid params creates a new Procedure
Failure/Error: expect {
count should have been changed by 1, but was changed by 0
# ./spec/controllers/procedures_controller_spec.rb:67:in `block (4 levels) in <top (required)>'

4) ProceduresController PUT update with valid params redirects to the procedure
Failure/Error: procedure = Procedure.create! valid_attributes
ActiveRecord::RecordInvalid:
Validation failed: Patient can't be blank, Surgeon can't be blank, Anaesthetist can't be blank, Hospital can't be blank
# ./spec/controllers/procedures_controller_spec.rb:120:in `block (4 levels) in <top (required)>'

5) ProceduresController PUT update with valid params assigns the requested procedure as @procedure
Failure/Error: procedure = Procedure.create! valid_attributes
ActiveRecord::RecordInvalid:
Validation failed: Patient can't be blank, Surgeon can't be blank, Anaesthetist can't be blank, Hospital can't be blank
# ./spec/controllers/procedures_controller_spec.rb:114:in `block (4 levels) in <top (required)>'

etc.

解决问题

我试图解决问题以了解我想做什么的尝试很糟糕。显而易见的解决方案是以某种方式在有效属性中包含指向有效患者/麻醉师/外科医生/医院的链接。我只需要知道正确的 RSpec/FactoryGirl/Rails (?) 语法来做这样的事情:

describe ProceduresController do

# This should return the minimal set of attributes required to create a valid
# Procedure. As you add validations to Procedure, be sure to
# adjust the attributes here as well.
FactoryGirl.create(:patient)
FactoryGirl.create(:surgeon)
FactoryGirl.create(:anaesthetist)
FactoryGirl.create(:hospital)
let(:valid_attributes) { { "description" => "MyText", "patient_id" => :patientid, "surgeon_id" => :surgeon.id, "anaesthetist_id" => :anaesthetist.id, "hospital_id" => :hospital.id } }

# This should return the minimal set of values that should be in the session
# in order to pass any filters (e.g. authentication) defined in
# ProceduresController. Be sure to keep this updated too.
let(:valid_session) { {} }

describe "GET index" do
it "assigns all procedures as @procedures" do
procedure = Procedure.create! valid_attributes
get :index, {}, valid_session
assigns(:procedures).should eq([procedure])
end
end

...

end

谢谢。

最佳答案

失败的是 POST createPUT update,从表面上看,由于 valid_attributes 不正确,记录没有被创建,所以你得出了一个很好的结论!

你可以这样处理它......

describe ProceduresController do
# This should return the minimal set of attributes required to create a valid
# Procedure. As you add validations to Procedure, be sure to
# adjust the attributes here as well.
let(:patient) { FactoryGirl.create(:patient) }
let(:surgeon) { FactoryGirl.create(:surgeon) }
let(:anaesthetist) { FactoryGirl.create(:anaesthetist)}
let(:hospital) { FactoryGirl.create(:hospital) }
let(:valid_attributes) { { "description" => "MyText",
"patient_id" => patient.id,
"surgeon_id" => surgeon.id,
"anaesthetist_id" => anaesthetist.id,
"hospital_id" => hospital.id } }

我希望您的创建和更新操作现在可以正常工作。

关于ruby-on-rails - RSpec valid_attributes 在验证相关模型时添加什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24570281/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com