【问题标题】:Ruby on Rails Rspec validationRuby on Rails Rspec 验证
【发布时间】:2017-06-30 15:49:37
【问题描述】:

验证

def active_consent_run
    return unless self.consent_run_id
    unless [
             ConsentRun::STATUS[:in_progress],
             ConsentRun::STATUS[:needs_witness_signature],
             ConsentRun::STATUS[:needs_researcher_signature]
           ].include?(self.consent_run.status)
      errors.add(:consent_run_id, 'needs to be in progress')
      false
    end
end

rspec 测试

it "#active_consent_run" do
  consent_run = create(:consent_run, :in_progress)
  consent_question = create(:consent_run_question,
     consent_run: consent_run)
  expect(consent_question.valid?).to eq false
  expect(consent_question.errors[:consent_run_id]).to \
     eq ('needs to be in progress')
end

我有这个验证,我在 rails 中编写了一个 rspec 测试,但测试一直在下降。有人能帮我吗?谢谢! :)

【问题讨论】:

  • 请分享您遇到了什么错误。
  • 失败/错误:expect(consent_question.valid?).to eq false expected: false got: true (比较使用 ==)
  • 您已经创建了consent_run(无论它是什么意思),并通过了:in_progress。如果您希望 in_progress 的测试失败,请传递另一个值。
  • 或者当我同意特征时,我得到这个错误:失败/错误:同意问题=创建(:同意运行问题,同意运行:同意运行)ActiveRecord::RecordInvalid:验证失败:同意运行需要进行
  • 我有这个统计数据: STATUS = { in_progress: 0, # ConsentRun 已开始,但尚未完成 needs_witness_signature: 1, # 患者已签名,但研究人员未签名 needs_researcher_signature: 2, # 患者已签名并见证(如果必需),但未经研究人员同意:3,# 用户同意拒绝_consent:4,# 患者不同意或未通过问题 remove_consent:5 # 患者同意后删除同意

标签: ruby-on-rails ruby rspec


【解决方案1】:

好的,看来您必须创建consent_run 状态为:in_progress然后 创建一个consent_question,并且只有然后 更新@ 的状态987654324@改成invalid:

it "#active_consent_run" do
  consent_run = create(:consent_run, :in_progress)
  consent_question = create(:consent_run_question,
    consent_run: consent_run)
  # I guess it’s an active record    ⇓ for illegal status
  consent_run.update_column :status, 4
  expect(consent_question.valid?).to eq false
  expect(consent_question.errors[:consent_run_id]).to \
    eq(['needs to be in progress'])
end 

【讨论】:

  • 失败/错误:consent_run.update_column :status, :removed_consent ActiveRecord::StatementInvalid: Mysql2::Error: 列 'status' 不能为空:UPDATE consent_runs SET consent_runs.status = NULL WHERE consent_runs.id = 94 我得到了这个错误
  • 好的,您可能已将此列作为整数存储在数据库中。我已经更新了答案。
  • 我们正在接近 :D 失败/错误:expect(consent_question.errors[:consent_run_id]).to eq ('needs to be in progress') 预期:“需要进行中”得到: ["需要进行中"]
  • 谢谢! :) 我可以发布另一个关于 sopes 的问题吗?相同的 rspec,ruby on rails 等
【解决方案2】:
scope :by_trial_id, -> trial_id { joins(consent_run: :consent_form).where("consent_forms.trial_id = ?", trial_id) }
  scope :by_patient_id, -> patient_id { joins(:consent_run).where("consent_runs.patient_id = ?", patient_id) }

我有这些范围,我需要编写一个 rspec 测试。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-09
    • 2011-07-04
    • 2016-08-04
    相关资源
    最近更新 更多