我目前有:
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
工作正常。
我想改变使用let!,所以我尝试了:
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
但它失败了,因为我现在对所有的@bp和@wp值都是零。
如何修复,即为期望值编写let语法正确?
答案 0 :(得分:1)
let!
正在设置本地方法,而不是实例变量,所以这有效:
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
答案 1 :(得分:1)
您需要使用wp
代替@wp
和bp
而不是@bp
,因为这些是方法,而不是实例变量:
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