国产一区二区精品久久_蜜桃狠狠狠狠狠狠狠狠狠_午夜视频精品_激情都市一区二区

當前位置:首頁 > 網站舊欄目 > 學習園地 > 設計軟件教程 > 每天一劑Rails良藥之Creating Dynamic Test Fixtures

每天一劑Rails良藥之Creating Dynamic Test Fixtures
2010-01-14 07:08:35  作者:  來源:
Rails的fixture文件在傳遞給YAML解析之前先用ERB解析,這樣一來我們就可以使用Ruby代碼動態生成測試數據,而不用一條數據一條數據的寫了:
Java代碼 復制代碼
  1. <% 1.upto(50do |number| %>   
  2. child_post_<%= number %>:   
  3.   id: <%= number + 3 %>   
  4.   title: This is auto-generated reply number <%= number %>   
  5.   body: We're on number <%= number %>   
  6.   created_at: 2006-01-30 08:03:56  
  7.   updated_at: 2006-01-30 08:03:56  
  8.   <%# Randomly choose a parent from a post we've already generated -%>   
  9.   parent_id: <%= rand(number - 1) + 1 %>   
  10.   user_id: <%= rand(5) + 1 %>   
  11. <% end %>  

我們還可以定義一些helper方法:
Java代碼 復制代碼
  1. <%   
  2.   def today   
  3.     Time.now.to_s(:db)   
  4.   end   
  5.   def next_week   
  6.     1.week.from_now.to_s(:db)   
  7.   end   
  8.   def last_week   
  9.     1.week.ago.to_s(:db)   
  10.   end   
  11. post_from_last_week:   
  12.   id: 60  
  13.   title: pizza   
  14.   body: Last night I had pizza. I readlly liked that story from AWDWR.   
  15.   created_at: <%= last_week %>   
  16.   updated_at: <%= last_week %>   
  17.   user_id: 1  
  18. post_created_in_future_should_not_display:   
  19.   id: 61  
  20.   title: Prognostication   
  21.   body: I predict that this post will show up next week.   
  22.   created_at: <%= next_week %>   
  23.   updated_at: <%= next_week %>   
  24. updated_post_displays_based_on_updated_time:   
  25.   id: 62  
  26.   title: This should show up as posted today.   
  27.   body: blah blah blah   
  28.   created_at: <%= last_week %>   
  29.   updated_at: <%= today %>   
  30.   user_id: 2  

安徽新華電腦學校專業職業規劃師為你提供更多幫助【在線咨詢