ruby – Rspec中’let’的范围是什么?
我尝试了以下方法:
describe "#check_recurring_and_send_message" do let(:schedule) {ScheduleKaya.new('test-client-id')} context "when it is 11AM and recurring event time is 10AM" do schedule.create_recurring_event('test-keyword','slack','day','10 AM') it "sends an SMS" do end it "set the next_occurrence to be for 10AM tomorrow" do tomorrow = Chronic.parse("tomorrow at 10AM") expect(schedule.next_occurrence).to eq(tomorrow) end end end 我在范围内遇到错误: `method_missing': `schedule` is not available on an example group (e.g. a `describe` or `context` block). It is only available from within individual examples (e.g. `it` blocks) or from constructs that run in the scope of an example (e.g. `before`,`let`,etc). (RSpec::Core::ExampleGroup::WrongScopeError) 不仅仅是这个例子,而且其他时候,我不完全理解let和在Rspec中创建实例的允许范围. 什么是让这里的用例与我创建使用schedule = blah blah? 我想我理解错误的字面意图:我不能仅在其中使用上下文中的计划.但是这个例子的正确方法是将东西置于描述,上下文或它之下以什么方式? 解决方法
让我们懒洋洋地评估一下,当你想在测试中共享一个变量时,这是很好的,但只有当测试需要它时.
来自文档:
由于这条线,你在这里找不到一个方法: schedule.create_recurring_event('test-keyword','10 AM') 您似乎希望在该上下文中阻止该行之前对其进行评估.你只需要像这样重写它: describe "#check_recurring_and_send_message" do let(:schedule) {ScheduleKaya.new('test-client-id')} context "when it is 11AM and recurring event time is 10AM" do before(:each) do schedule.create_recurring_event('test-keyword','10 AM') end it "sends an SMS" do end it "set the next_occurrence to be for 10AM tomorrow" do tomorrow = Chronic.parse("tomorrow at 10AM") expect(schedule.next_occurrence).to eq(tomorrow) end end end (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |