Reputation: 96594
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?
Upvotes: 0
Views: 92
Reputation: 40576
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
Upvotes: 1
Reputation: 96594
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
Upvotes: 1