How to declare a variable shared between examples in RSpec?

后端 未结 3 1674
伪装坚强ぢ
伪装坚强ぢ 2021-02-01 14:01

Suppose I have the following spec:

...
describe Thing do

  it \'can read data\' do
     @data = get_data_from_file  # [ \'42\', \'36\' ]
     expect(@data.count         


        
3条回答
  •  情深已故
    2021-02-01 14:25

    This is really the purpose of the RSpec let helper which allows you to do this with your code:

    ...
    describe Thing do
      let(:data) { get_data_from_file }
    
      it 'can read data' do
         expect(data.count).to eq 2
      end
    
      it 'can process data' do
         expect(data[0].to_i).to eq 42
      end
    
    end
    ...
    

提交回复
热议问题