加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby – Rspec中’let’的范围是什么?

发布时间:2020-12-17 03:18:25 所属栏目:百科 来源:网络整理
导读:我尝试了以下方法: 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','da
我尝试了以下方法:

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?

我想我理解错误的字面意图:我不能仅在其中使用上下文中的计划.但是这个例子的正确方法是将东西置于描述,上下文或它之下以什么方式?

解决方法

让我们懒洋洋地评估一下,当你想在测试中共享一个变量时,这是很好的,但只有当测试需要它时.

来自文档:

Use let to define a memoized helper method. The value will be cached
across multiple calls in the same example but not across examples.

Note that let is lazy-evaluated: it is not evaluated until the first
time the method it defines is invoked. You can use let! to force the
method’s invocation before each example.

By default,let is threadsafe,but you can configure it not to be by
disabling config.threadsafe,which makes let perform a bit faster.

由于这条线,你在这里找不到一个方法:

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读