我有一個名為 Jurisdiction 的表,并創建了該任務來更新 kind 列:
namespace :populate_jurisdiction do
desc "Populate column kind of jurisdiction model"
task kind: :environment do
Jurisdiction.all.each { |jurisdiction|
case (jurisdiction.kind.nil? || jurisdiction.kind.blank?)
when jurisdiction.name.parameterize == "federal"
jurisdiction.kind = 1
when jurisdiction.name.parameterize == "estadual"
jurisdiction.kind = 2
when jurisdiction.name.parameterize == "municipal"
jurisdiction.kind = 3
when jurisdiction.name.parameterize == "privado"
jurisdiction.kind = 4
end
jurisdiction.save!
}
end
end
然后我創建了那個測驗
require "spec_helper"
Rails.application.load_tasks
describe "populate_jurisdiction:kind" do
context "when update kind column of jurisdiction" do
let(:federal) { create(:jurisdiction, name: "Federal", kind: nil) }
let(:state) { create(:jurisdiction, name: "Estadual", kind: nil) }
let(:municipal) { create(:jurisdiction, name: "Municipal", kind: '') }
let(:particular) { create(:jurisdiction, name: "Privado", kind: '') }
it "when kind column is updated" do
Rake::Task["populate_jurisdiction:kind"].invoke
expect(federal.kind).to eq(1)
expect(state.kind).to eq(2)
expect(municipal.kind).to eq(3)
expect(particular.kind).to eq(4)
end
end
end
當我在 Rails 控制臺中運行任務時,它可以作業,但是當我運行測驗時,出現此錯誤
Failures:
1) populate_jurisdiction:kind when update kind column of jurisdiction when kind column is updated
Failure/Error: expect(federal.kind).to eq(1)
expected: 1
got: nil
(compared using ==)
我做錯了什么?我該如何解決這個測驗?
uj5u.com熱心網友回復:
在創建任何司法管轄區之前,您正在呼叫 rake 任務。當你這樣說時:
let(:federal) { create(:jurisdiction, name: "Federal", kind: nil) }
這將federal在您第一次在測驗中訪問它時創建,然后在測驗期間記住它。因此,federal在您的 rake 任務運行之前沒有。如果您在任務運行后使用let!而不是let重新加載管轄區,您將獲得更好的結果。
順便說一句,你的 rake 任務不像你認為的那樣作業。有兩種形式case:
case expr
when value1
...
when value2
...
end
和
case
when expr1
...
when expr2
...
end
當您打算使用第二種形式時,您使用的是第一種形式,而您的代碼是偶然作業的。我懷疑你所有的kinds 都是nil當你運行它時,否則你最終會這樣做:
case false
...
end
你會進入jurisdiction.name.parameterize測驗失敗的第一個分支。
你的任務應該看起來更像:
Jurisdiction.all.reject { |j| j.kind.blank? }.each do |jurisdiction|
case jurisdiction.name.parameterize
when 'federal'
jurisdiction.kind = 1
when 'estadual'
jurisdiction.kind = 2
when 'municipal'
jurisdiction.kind = 3
when 'privado'
jurisdiction.kind = 4
end
jurisdiction.save!
end
或者:
Jurisdiction.all.reject { |j| j.kind.blank? }.each do |jurisdiction|
jurisdiction.kind = case jurisdiction.name.parameterize
when 'federal' then 1
when 'estadual' then 2
when 'municipal' then 3
when 'privado' then 4
end
jurisdiction.save!
end
如果kind是一個整數,那么只有當它可以被推入資料庫kind.blank?時??才會為真:kind.nil?
Jurisdiction.where(kind: nil).each do |jurisdiction|
jurisdiction.kind = case jurisdiction.name.parameterize
when 'federal' then 1
when 'estadual' then 2
when 'municipal' then 3
when 'privado' then 4
end
jurisdiction.save!
end
并且看起來#parameterize在這種情況下只會將轉換name為小寫,因此將所有邏輯推入資料庫:
# This is an SQL CASE expression, not a Ruby one
Jurisdiction.where(kind: nil).update_all(%q(
kind = case lower(name)
when 'federal' then 1
when 'estadual' then 2
when 'municipal' then 3
when 'privado' then 4
end
))
uj5u.com熱心網友回復:
當您呼叫 rake 任務時,沒有管轄權,這就是您得到nil. 例如,聯邦管轄權僅在您呼叫 rake 任務之后創建federal.kind。
require "spec_helper"
Rails.application.load_tasks
describe "populate_jurisdiction:kind" do
context "when update kind column of jurisdiction" do
let(:federal) { create(:jurisdiction, name: "Federal", kind: nil) }
let(:state) { create(:jurisdiction, name: "Estadual", kind: nil) }
let(:municipal) { create(:jurisdiction, name: "Municipal", kind: '') }
let(:particular) { create(:jurisdiction, name: "Privado", kind: '') }
it "when kind column is updated" do
# NOTE: jurisdictions are not created until `let` blocks are called.
federal
state
municipal
particular
Rake::Task["populate_jurisdiction:kind"].invoke
# NOTE: `let` return values are memoized, second call will just
# retrieve the value. You have to reload the models as well
# to get the updated values from the database.
expect(federal.reload.kind).to eq(1)
expect(state.reload.kind).to eq(2)
expect(municipal.reload.kind).to eq(3)
expect(particular.reload.kind).to eq(4)
end
end
end
https://relishapp.com/rspec/rspec-core/v/3-11/docs/helper-methods/let-and-let
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/470374.html
上一篇:是否有pythonic單元測驗方法來斷言函式已成功匯入?
下一篇:在Python中測驗修飾函式
