Вопрос

I currently have:

describe 'least number of moves from x to y' do

  it 'has a populated chessboard' do
    @wp='white-pawn'
    @bp='black-pawn'

    expect(ChessBoard.new.populate_new_board).to eq [
      ['white-castle','white-knight','white-bishop','white-queen','white-king','white-bishop','white-knight','white-castle'],
      [@wp,@wp,@wp,@wp,@wp,@wp,@wp,@wp],
      [nil,nil,nil,nil,nil,nil,nil,nil],
      [nil,nil,nil,nil,nil,nil,nil,nil],
      [nil,nil,nil,nil,nil,nil,nil,nil],
      [nil,nil,nil,nil,nil,nil,nil,nil],
      [@bp,@bp,@bp,@bp,@bp,@bp,@bp,@bp],
      ['black-castle','black-knight','black-bishop','black-king','black-queen','black-bishop','black-knight','black-castle']]
  end 

which works ok.
I want to change to use let!, so I tried:

describe 'least number of moves from x to y' do

  let!(:wp){'white-pawn'}
  let!(:bp){'black-pawn'}

  it 'has a populated chessboard' do
    expect(ChessBoard.new.populate_new_board).to eq [
      ['white-castle','white-knight','white-bishop','white-queen','white-king','white-bishop','white-knight','white-castle'],
      [@wp,@wp,@wp,@wp,@wp,@wp,@wp,@wp],
      [nil,nil,nil,nil,nil,nil,nil,nil],
      [nil,nil,nil,nil,nil,nil,nil,nil],
      [nil,nil,nil,nil,nil,nil,nil,nil],
      [nil,nil,nil,nil,nil,nil,nil,nil],
      [@bp,@bp,@bp,@bp,@bp,@bp,@bp,@bp],
      ['black-castle','black-knight','black-bishop','black-king','black-queen','black-bishop','black-knight','black-castle']]
  end 

but it fails because I now get nil for all the @bp and @wp values.

How to fix, i.e. write the let syntax correct for the expect?

Это было полезно?

Решение

You'll need to use wp instead of @wp and bp instead of @bp, because those are methods, not instance variables:

describe 'least number of moves from x to y' do

  let!(:wp){'white-pawn'}
  let!(:bp){'black-pawn'}

  it 'has a populated chessboard' do

    expect(ChessBoard.new.populate_new_board).to eq [
      ['white-castle','white-knight','white-bishop','white-queen','white-king','white-bishop','white-knight','white-castle'],
      [wp,wp,wp,wp,wp,wp,wp,wp],
      [nil,nil,nil,nil,nil,nil,nil,nil],
      [nil,nil,nil,nil,nil,nil,nil,nil],
      [nil,nil,nil,nil,nil,nil,nil,nil],
      [nil,nil,nil,nil,nil,nil,nil,nil],
      [bp,bp,bp,bp,bp,bp,bp,bp],
      ['black-castle','black-knight','black-bishop','black-king','black-queen','black-bishop','black-knight','black-castle']]
  end
end

Другие советы

let! was setting up local methods, not instance variables so this worked:

 it 'has a populated chessboard' do
    expect(ChessBoard.new.populate_new_board).to eq [
      ['white-castle','white-knight','white-bishop','white-queen','white-king','white-bishop','white-knight','white-castle'],
      [wp,wp,wp,wp,wp,wp,wp,wp],
      [nil,nil,nil,nil,nil,nil,nil,nil],
      [nil,nil,nil,nil,nil,nil,nil,nil],
      [nil,nil,nil,nil,nil,nil,nil,nil],
      [nil,nil,nil,nil,nil,nil,nil,nil],
      [bp,bp,bp,bp,bp,bp,bp,bp],
      ['black-castle','black-knight','black-bishop','black-king','black-queen','black-bishop','black-knight','black-castle']]
  end 
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top