在验证相关模型时要向RSpec valid_attributes添加什么

Set*_*ies 3 rspec ruby-on-rails rspec-rails ruby-on-rails-4

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

使用rake生成脚手架model_name我已经制作了大量的代码似乎有些意义,但我并不十分自信.

在我添加一些数据库关系和模型验证之前,我一直在使用RSpec,然后这一切都爆炸了,我真的不知道修复它的最佳实践或最干净的方法是什么.

很明显我的问题是"有效属性"需要用链接到有效的患者/麻醉师/程序/外科医生的外键来定义,但我不知道我应该如何在RSpec中写这个(可能使用FactoryGirl来生成有效的每个模型的对象?)模型RSpec代码对我有意义,但控制器代码超出了我的深度.

看起来它应该可以在一行,或者可能是四行(每个对象一行),但应该是它.

代码

#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
Run Code Online (Sandbox Code Playgroud)
#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
Run Code Online (Sandbox Code Playgroud)
#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
Run Code Online (Sandbox Code Playgroud)
#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.
Run Code Online (Sandbox Code Playgroud)

解决问题

我很难尝试解决问题,以便了解我想要做什么.显而易见的解决方案是以某种方式包含有效属性中有效的患者/麻醉师/外科医生/医院的链接.我只需要知道正确的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
Run Code Online (Sandbox Code Playgroud)

谢谢.

Ste*_*zyn 5

这是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 } }
Run Code Online (Sandbox Code Playgroud)

我希望您的创建和更新操作现在可以正常运行.