【问题标题】:rspec-given error `Then` is not available from within an example (e.g. an `it` block)rspec-given 错误 `Then` 在示例中不可用(例如 `it` 块)
【发布时间】:2015-10-06 05:11:48
【问题描述】:

我正在使用 rspec-given 并不断收到此错误。

失败/错误:然后 { Then 在示例中(例如 it 块)或在示例范围内运行的构造(例如 beforelet 等)中不可用。它仅适用于示例组(例如 describecontext 块)。

describe SchoolService do
  Given(:school) { create(:school_with_applications) }
  Given(:service) { School.new(@school) }

  describe 'create_default_programs_and_year_grades!' do
    it 'checks program size' do 
      When { service.create_default_programs_and_year_grades! }
      Then { expect(school.programs.size).to eq 3 }
    end
  end
end

【问题讨论】:

    标签: ruby-on-rails ruby ruby-on-rails-4 rspec rspec-rails


    【解决方案1】:

    错误信息说明了一切:

    Then is not available from within an example (e.g. an it block) or from constructs that run in the scope of an example (e.g. before, let, etc). It is only available on an example group (e.g. a describe or context block).
    

    请仔细阅读错误信息。并且您在错误消息本身中有解决方案。

    您不能在it 块内使用Then,您只能将Thendescribecontext 块一起使用。

    所以,要解决您的问题,只需使用context 而不是it

    describe SchoolService do
      Given(:school) { create(:school_with_applications) }
      Given(:service) { School.new(@school) }
    
      describe 'create_default_programs_and_year_grades!' do
        context 'checks program size' do 
          When { service.create_default_programs_and_year_grades! }
          Then { expect(school.programs.size).to eq 3 }
        end
      end
    end
    

    查看更多examples here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-08-11
      • 1970-01-01
      • 2011-07-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多