The rspec-specified error `Then` is not available from the example (for example,` it`)

I am using rspec-given and keep getting this error.

Error / Error: Then { Then not available from the example (for example, the it block) or from constructs that are executed in the scope of the example (for example, before , let , etc.). It is available only in the group of examples (for example, in the describe or context block).

 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 
+5
source share
1 answer

The error message says it all:

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

read the error message carefully. And you have a solution in the error message itself.

You cannot use Then inside the it block, you can use Then only with describe or context .

So, to solve your problem, just use context instead of 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 

More examples here .

+3
source

Source: https://habr.com/ru/post/1233017/


All Articles