1

当我wish单独测试对象时,它可以工作:

before do
  @wish1 = FactoryGirl.create(:wish)
  @wish2 = FactoryGirl.create(:wish)
  @wish3 = FactoryGirl.create(:wish)
  visit wishes_path
end

it { should have_link(@wish1.destination)}
it { should have_link(@wish2.destination)}
it { should have_link(@wish3.destination)}

但是当我用数组尝试它并循环遍历时each,它返回 nil。

[@wish1, @wish2, @wish3].each do |w|
   it { should have_link(w.destination)}
end

结果

NoMethodError: undefined method `destination' for nil:NilClass

有什么方法可以改进这个测试吗?我的页面应该有很多Wishes的链接,而且大部分都不一样,所以会有很多重复。但我不能让它更简单。

4

1 回答 1

1
let!(:wishes) { FactoryGirl.create_list(:wish, 3) }

it 'pages includes all whishes links' do
  visit wishes_path
  wishes.each do |wish|
    page.should have_link(wish.destination)
  end
end
于 2013-10-12T13:30:43.650 回答