有沒有辦法給let變量名添加一個序列?的排序是這樣的:讓變量名稱與序列
5.times do |n|
let (:"item_'#{n}'") { FactoryGirl.create(:item, name: "Item-'#{n}'") }
end
然後像這樣的測試可以工作:
5.times do |n|
it { should have_link("Item-'#{n}'", href: item_path("item_'#{n}'") }
end
這將導致對適當的分類測試,但只是想了解的基礎知識。
編輯: 有一個錯字,我刪除了單引號和讓利通話似乎是工作
let! (:"item_#{n}") { FactoryGirl.create(:item, name: "Item-#{n}") }
測試通過單個情況下,如果我使用:
it { should have_link("Item-0", href: item_path(item_0)
但不適用於如果我使用的序列:
it { should have_link("Item-#{n}", href: item_path("item_#{n}")
我已經驗證問題是在href路徑中。如何在路徑中使用時插入item_n?
所以我想答案是可能的,但不值得。感謝您解釋字符串與ruby可變部分。這就說得通了。我認爲你需要從''Item - '#{n}'「'中刪除單引號。早些時候,這不適合我。 – 2012-07-13 07:20:41
我試過這個,它不起作用。我得到異常:'#'。 @項目是零。但是,'Item.all.each'確實可以用於我的設置。 –
2012-07-31 03:38:52