RSpec 要求我在测试通过之前打印对象
RSpec requires me to print object before test can pass
我有一个简单的模型测试,我正在测试我的验证,出于某种原因,我需要在我的测试通过之前打印对象。
以下测试失败(失败输出如下所示):
RSpec.describe HealthProfile, type: :model do
let(:client) { create(:client) }
let(:intro_hp) { create(:health_profile, :no_callbacks, :with_issues, client_id: client.id)}
describe "should validate that" do
it "diabetes is required in exit if present in intro" do
exit_hp = build(:health_profile, :end, :no_callbacks, client_id: client.id)
exit_hp.valid?
expect(exit_hp.errors.keys).to include(:diabetic)
end
end
end
Failures:
1) HealthProfile should validate that diabetes is required in exit if present in intro
Failure/Error: expect(exit_hp.errors.keys).to include(:diabetic)
expected [:weight_mindset_one, :weight_mindset_two, :weight_mindset_three] to include :diabetic
Diff:
@@ -1,2 +1,2 @@
-[:diabetic]
+[:weight_mindset_one, :weight_mindset_two, :weight_mindset_three]
# ./spec/models/health_profile_spec.rb:14:in `block (3 levels) in <top (required)>'
添加打印语句后,p intro_hp
,相同的测试通过:
RSpec.describe HealthProfile, type: :model do
let(:client) { create(:client) }
let(:intro_hp) { create(:health_profile, :no_callbacks, :with_issues, client_id: client.id)}
describe "should validate that" do
it "diabetes is required in exit if present in intro" do
exit_hp = build(:health_profile, :end, :no_callbacks, client_id: client.id)
p intro_hp
exit_hp.valid?
expect(exit_hp.errors.keys).to include(:diabetic)
end
end
end
为什么会发生这种情况,我该如何避免?
intro_hp
在第一种情况下没有被实例化,因为 let
是惰性的——只有在使用时才实例化对象。
您可以使用let!
立即实例化对象:
RSpec.describe HealthProfile, type: :model do
let(:client) { create(:client) }
let!(:intro_hp) { create(:health_profile, :no_callbacks, :with_issues, client_id: client.id)}
关于相关说明 - 您不必打印它,您可以直接使用它:
it "diabetes is required in exit if present in intro" do
intro_hp
exit_hp = build(:health_profile, :end, :no_callbacks, client_id: client.id)
exit_hp.valid?
expect(exit_hp.errors.keys).to include(:diabetic)
end
我有一个简单的模型测试,我正在测试我的验证,出于某种原因,我需要在我的测试通过之前打印对象。
以下测试失败(失败输出如下所示):
RSpec.describe HealthProfile, type: :model do
let(:client) { create(:client) }
let(:intro_hp) { create(:health_profile, :no_callbacks, :with_issues, client_id: client.id)}
describe "should validate that" do
it "diabetes is required in exit if present in intro" do
exit_hp = build(:health_profile, :end, :no_callbacks, client_id: client.id)
exit_hp.valid?
expect(exit_hp.errors.keys).to include(:diabetic)
end
end
end
Failures:
1) HealthProfile should validate that diabetes is required in exit if present in intro
Failure/Error: expect(exit_hp.errors.keys).to include(:diabetic)
expected [:weight_mindset_one, :weight_mindset_two, :weight_mindset_three] to include :diabetic
Diff:
@@ -1,2 +1,2 @@
-[:diabetic]
+[:weight_mindset_one, :weight_mindset_two, :weight_mindset_three]
# ./spec/models/health_profile_spec.rb:14:in `block (3 levels) in <top (required)>'
添加打印语句后,p intro_hp
,相同的测试通过:
RSpec.describe HealthProfile, type: :model do
let(:client) { create(:client) }
let(:intro_hp) { create(:health_profile, :no_callbacks, :with_issues, client_id: client.id)}
describe "should validate that" do
it "diabetes is required in exit if present in intro" do
exit_hp = build(:health_profile, :end, :no_callbacks, client_id: client.id)
p intro_hp
exit_hp.valid?
expect(exit_hp.errors.keys).to include(:diabetic)
end
end
end
为什么会发生这种情况,我该如何避免?
intro_hp
在第一种情况下没有被实例化,因为 let
是惰性的——只有在使用时才实例化对象。
您可以使用let!
立即实例化对象:
RSpec.describe HealthProfile, type: :model do
let(:client) { create(:client) }
let!(:intro_hp) { create(:health_profile, :no_callbacks, :with_issues, client_id: client.id)}
关于相关说明 - 您不必打印它,您可以直接使用它:
it "diabetes is required in exit if present in intro" do
intro_hp
exit_hp = build(:health_profile, :end, :no_callbacks, client_id: client.id)
exit_hp.valid?
expect(exit_hp.errors.keys).to include(:diabetic)
end