我正在测试rails中的 cron 作业,我想测试对象的属性是否默认为false,这是执行该作业的代码部分:
describe ".perform()" do
before :each do
SendReportCronJob.perform()
end
context "automatically_send_report" do
it "should be false by default" do
binding.pry()
expect( automatically_send_report.value ).to be "false"
end
end
context "time_limit" do
it "should not be nil" do
expect( time_limit_for_sending_report.value ).to_not be_nil
end
end
end
问题是 automatic_send_report.value 返回“false”值!
这里是我执行binding.pry()
时的rails控制台输出:
#<Setting:0x00000009990878> {
:id => "69617295-4209-4092-80cf-5934d1cf7d38",
:related_id => "cd830ace-933a-4230-ad54-bd94e63d5d7b",
:key => "automatically_send_report",
:value => "false",
:data_type => "boolean",
:is_archived => false,
:updated_by_id => nil,
:device_id => nil,
:created_at => Wed, 18 Apr 2018 18:34:35 +03 +03:00,
:updated_at => Wed, 18 Apr 2018 18:34:35 +03 +03:00
}
我可以在rspec
文件中将此值从字符串转换为布尔值吗?
答案 0 :(得分:1)
您不需要将字符串转换为布尔值,您也可以比较字符串,但在这种情况下,您需要使用eq
而不是be
,因为它们是不同的对象具有相同的值:
expect(automatically_send_report.value).to eq "false"