openskies 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (494) hide show
  1. data/CHANGELOG.md +87 -0
  2. data/README.rdoc +35 -0
  3. data/bin/openskies +9 -0
  4. data/guides/assets/images/belongs_to.png +0 -0
  5. data/guides/assets/images/book_icon.gif +0 -0
  6. data/guides/assets/images/bullet.gif +0 -0
  7. data/guides/assets/images/challenge.png +0 -0
  8. data/guides/assets/images/chapters_icon.gif +0 -0
  9. data/guides/assets/images/check_bullet.gif +0 -0
  10. data/guides/assets/images/credits_pic_blank.gif +0 -0
  11. data/guides/assets/images/csrf.png +0 -0
  12. data/guides/assets/images/customized_error_messages.png +0 -0
  13. data/guides/assets/images/edge_badge.png +0 -0
  14. data/guides/assets/images/error_messages.png +0 -0
  15. data/guides/assets/images/feature_tile.gif +0 -0
  16. data/guides/assets/images/footer_tile.gif +0 -0
  17. data/guides/assets/images/fxn.png +0 -0
  18. data/guides/assets/images/grey_bullet.gif +0 -0
  19. data/guides/assets/images/habtm.png +0 -0
  20. data/guides/assets/images/has_many.png +0 -0
  21. data/guides/assets/images/has_many_through.png +0 -0
  22. data/guides/assets/images/has_one.png +0 -0
  23. data/guides/assets/images/has_one_through.png +0 -0
  24. data/guides/assets/images/header_backdrop.png +0 -0
  25. data/guides/assets/images/header_tile.gif +0 -0
  26. data/guides/assets/images/i18n/demo_html_safe.png +0 -0
  27. data/guides/assets/images/i18n/demo_localized_pirate.png +0 -0
  28. data/guides/assets/images/i18n/demo_translated_en.png +0 -0
  29. data/guides/assets/images/i18n/demo_translated_pirate.png +0 -0
  30. data/guides/assets/images/i18n/demo_translation_missing.png +0 -0
  31. data/guides/assets/images/i18n/demo_untranslated.png +0 -0
  32. data/guides/assets/images/icons/README +5 -0
  33. data/guides/assets/images/icons/callouts/1.png +0 -0
  34. data/guides/assets/images/icons/callouts/10.png +0 -0
  35. data/guides/assets/images/icons/callouts/11.png +0 -0
  36. data/guides/assets/images/icons/callouts/12.png +0 -0
  37. data/guides/assets/images/icons/callouts/13.png +0 -0
  38. data/guides/assets/images/icons/callouts/14.png +0 -0
  39. data/guides/assets/images/icons/callouts/15.png +0 -0
  40. data/guides/assets/images/icons/callouts/2.png +0 -0
  41. data/guides/assets/images/icons/callouts/3.png +0 -0
  42. data/guides/assets/images/icons/callouts/4.png +0 -0
  43. data/guides/assets/images/icons/callouts/5.png +0 -0
  44. data/guides/assets/images/icons/callouts/6.png +0 -0
  45. data/guides/assets/images/icons/callouts/7.png +0 -0
  46. data/guides/assets/images/icons/callouts/8.png +0 -0
  47. data/guides/assets/images/icons/callouts/9.png +0 -0
  48. data/guides/assets/images/icons/caution.png +0 -0
  49. data/guides/assets/images/icons/example.png +0 -0
  50. data/guides/assets/images/icons/home.png +0 -0
  51. data/guides/assets/images/icons/important.png +0 -0
  52. data/guides/assets/images/icons/next.png +0 -0
  53. data/guides/assets/images/icons/note.png +0 -0
  54. data/guides/assets/images/icons/prev.png +0 -0
  55. data/guides/assets/images/icons/tip.png +0 -0
  56. data/guides/assets/images/icons/up.png +0 -0
  57. data/guides/assets/images/icons/warning.png +0 -0
  58. data/guides/assets/images/jaimeiniesta.jpg +0 -0
  59. data/guides/assets/images/nav_arrow.gif +0 -0
  60. data/guides/assets/images/polymorphic.png +0 -0
  61. data/guides/assets/images/posts_index.png +0 -0
  62. data/guides/assets/images/radar.png +0 -0
  63. data/guides/assets/images/rails_guides_kindle_cover.jpg +0 -0
  64. data/guides/assets/images/rails_guides_logo.gif +0 -0
  65. data/guides/assets/images/rails_logo_remix.gif +0 -0
  66. data/guides/assets/images/rails_welcome.png +0 -0
  67. data/guides/assets/images/session_fixation.png +0 -0
  68. data/guides/assets/images/tab_grey.gif +0 -0
  69. data/guides/assets/images/tab_info.gif +0 -0
  70. data/guides/assets/images/tab_note.gif +0 -0
  71. data/guides/assets/images/tab_red.gif +0 -0
  72. data/guides/assets/images/tab_yellow.gif +0 -0
  73. data/guides/assets/images/tab_yellow.png +0 -0
  74. data/guides/assets/images/validation_error_messages.png +0 -0
  75. data/guides/assets/images/vijaydev.jpg +0 -0
  76. data/guides/assets/javascripts/guides.js +7 -0
  77. data/guides/assets/javascripts/syntaxhighlighter/shBrushAS3.js +59 -0
  78. data/guides/assets/javascripts/syntaxhighlighter/shBrushAppleScript.js +75 -0
  79. data/guides/assets/javascripts/syntaxhighlighter/shBrushBash.js +59 -0
  80. data/guides/assets/javascripts/syntaxhighlighter/shBrushCSharp.js +65 -0
  81. data/guides/assets/javascripts/syntaxhighlighter/shBrushColdFusion.js +100 -0
  82. data/guides/assets/javascripts/syntaxhighlighter/shBrushCpp.js +97 -0
  83. data/guides/assets/javascripts/syntaxhighlighter/shBrushCss.js +91 -0
  84. data/guides/assets/javascripts/syntaxhighlighter/shBrushDelphi.js +55 -0
  85. data/guides/assets/javascripts/syntaxhighlighter/shBrushDiff.js +41 -0
  86. data/guides/assets/javascripts/syntaxhighlighter/shBrushErlang.js +52 -0
  87. data/guides/assets/javascripts/syntaxhighlighter/shBrushGroovy.js +67 -0
  88. data/guides/assets/javascripts/syntaxhighlighter/shBrushJScript.js +52 -0
  89. data/guides/assets/javascripts/syntaxhighlighter/shBrushJava.js +57 -0
  90. data/guides/assets/javascripts/syntaxhighlighter/shBrushJavaFX.js +58 -0
  91. data/guides/assets/javascripts/syntaxhighlighter/shBrushPerl.js +72 -0
  92. data/guides/assets/javascripts/syntaxhighlighter/shBrushPhp.js +88 -0
  93. data/guides/assets/javascripts/syntaxhighlighter/shBrushPlain.js +33 -0
  94. data/guides/assets/javascripts/syntaxhighlighter/shBrushPowerShell.js +74 -0
  95. data/guides/assets/javascripts/syntaxhighlighter/shBrushPython.js +64 -0
  96. data/guides/assets/javascripts/syntaxhighlighter/shBrushRuby.js +55 -0
  97. data/guides/assets/javascripts/syntaxhighlighter/shBrushSass.js +94 -0
  98. data/guides/assets/javascripts/syntaxhighlighter/shBrushScala.js +51 -0
  99. data/guides/assets/javascripts/syntaxhighlighter/shBrushSql.js +66 -0
  100. data/guides/assets/javascripts/syntaxhighlighter/shBrushVb.js +56 -0
  101. data/guides/assets/javascripts/syntaxhighlighter/shBrushXml.js +69 -0
  102. data/guides/assets/javascripts/syntaxhighlighter/shCore.js +17 -0
  103. data/guides/assets/stylesheets/fixes.css +16 -0
  104. data/guides/assets/stylesheets/kindle.css +11 -0
  105. data/guides/assets/stylesheets/main.css +453 -0
  106. data/guides/assets/stylesheets/print.css +52 -0
  107. data/guides/assets/stylesheets/reset.css +43 -0
  108. data/guides/assets/stylesheets/style.css +13 -0
  109. data/guides/assets/stylesheets/syntaxhighlighter/shCore.css +226 -0
  110. data/guides/assets/stylesheets/syntaxhighlighter/shCoreDefault.css +328 -0
  111. data/guides/assets/stylesheets/syntaxhighlighter/shCoreDjango.css +331 -0
  112. data/guides/assets/stylesheets/syntaxhighlighter/shCoreEclipse.css +339 -0
  113. data/guides/assets/stylesheets/syntaxhighlighter/shCoreEmacs.css +324 -0
  114. data/guides/assets/stylesheets/syntaxhighlighter/shCoreFadeToGrey.css +328 -0
  115. data/guides/assets/stylesheets/syntaxhighlighter/shCoreMDUltra.css +324 -0
  116. data/guides/assets/stylesheets/syntaxhighlighter/shCoreMidnight.css +324 -0
  117. data/guides/assets/stylesheets/syntaxhighlighter/shCoreRDark.css +324 -0
  118. data/guides/assets/stylesheets/syntaxhighlighter/shThemeDefault.css +117 -0
  119. data/guides/assets/stylesheets/syntaxhighlighter/shThemeDjango.css +120 -0
  120. data/guides/assets/stylesheets/syntaxhighlighter/shThemeEclipse.css +128 -0
  121. data/guides/assets/stylesheets/syntaxhighlighter/shThemeEmacs.css +113 -0
  122. data/guides/assets/stylesheets/syntaxhighlighter/shThemeFadeToGrey.css +117 -0
  123. data/guides/assets/stylesheets/syntaxhighlighter/shThemeMDUltra.css +113 -0
  124. data/guides/assets/stylesheets/syntaxhighlighter/shThemeMidnight.css +113 -0
  125. data/guides/assets/stylesheets/syntaxhighlighter/shThemeRDark.css +113 -0
  126. data/guides/assets/stylesheets/syntaxhighlighter/shThemeRailsGuides.css +116 -0
  127. data/guides/code/getting_started/Gemfile +38 -0
  128. data/guides/code/getting_started/README.rdoc +261 -0
  129. data/guides/code/getting_started/Rakefile +7 -0
  130. data/guides/code/getting_started/app/assets/images/rails.png +0 -0
  131. data/guides/code/getting_started/app/assets/javascripts/application.js +15 -0
  132. data/guides/code/getting_started/app/assets/javascripts/comments.js.coffee +3 -0
  133. data/guides/code/getting_started/app/assets/javascripts/home.js.coffee +3 -0
  134. data/guides/code/getting_started/app/assets/javascripts/posts.js.coffee +3 -0
  135. data/guides/code/getting_started/app/assets/stylesheets/application.css +13 -0
  136. data/guides/code/getting_started/app/assets/stylesheets/comments.css.scss +3 -0
  137. data/guides/code/getting_started/app/assets/stylesheets/home.css.scss +3 -0
  138. data/guides/code/getting_started/app/assets/stylesheets/posts.css.scss +3 -0
  139. data/guides/code/getting_started/app/assets/stylesheets/scaffolds.css.scss +56 -0
  140. data/guides/code/getting_started/app/controllers/application_controller.rb +3 -0
  141. data/guides/code/getting_started/app/controllers/comments_controller.rb +16 -0
  142. data/guides/code/getting_started/app/controllers/home_controller.rb +5 -0
  143. data/guides/code/getting_started/app/controllers/posts_controller.rb +84 -0
  144. data/guides/code/getting_started/app/helpers/application_helper.rb +2 -0
  145. data/guides/code/getting_started/app/helpers/comments_helper.rb +2 -0
  146. data/guides/code/getting_started/app/helpers/home_helper.rb +2 -0
  147. data/guides/code/getting_started/app/helpers/posts_helper.rb +5 -0
  148. data/guides/code/getting_started/app/models/comment.rb +3 -0
  149. data/guides/code/getting_started/app/models/post.rb +11 -0
  150. data/guides/code/getting_started/app/models/tag.rb +3 -0
  151. data/guides/code/getting_started/app/views/comments/_comment.html.erb +15 -0
  152. data/guides/code/getting_started/app/views/comments/_form.html.erb +13 -0
  153. data/guides/code/getting_started/app/views/home/index.html.erb +2 -0
  154. data/guides/code/getting_started/app/views/layouts/application.html.erb +14 -0
  155. data/guides/code/getting_started/app/views/posts/_form.html.erb +32 -0
  156. data/guides/code/getting_started/app/views/posts/edit.html.erb +6 -0
  157. data/guides/code/getting_started/app/views/posts/index.html.erb +27 -0
  158. data/guides/code/getting_started/app/views/posts/new.html.erb +5 -0
  159. data/guides/code/getting_started/app/views/posts/show.html.erb +31 -0
  160. data/guides/code/getting_started/app/views/tags/_form.html.erb +12 -0
  161. data/guides/code/getting_started/config/application.rb +59 -0
  162. data/guides/code/getting_started/config/boot.rb +6 -0
  163. data/guides/code/getting_started/config/database.yml +25 -0
  164. data/guides/code/getting_started/config/environment.rb +5 -0
  165. data/guides/code/getting_started/config/environments/development.rb +37 -0
  166. data/guides/code/getting_started/config/environments/production.rb +67 -0
  167. data/guides/code/getting_started/config/environments/test.rb +37 -0
  168. data/guides/code/getting_started/config/initializers/backtrace_silencers.rb +7 -0
  169. data/guides/code/getting_started/config/initializers/inflections.rb +15 -0
  170. data/guides/code/getting_started/config/initializers/mime_types.rb +5 -0
  171. data/guides/code/getting_started/config/initializers/secret_token.rb +7 -0
  172. data/guides/code/getting_started/config/initializers/session_store.rb +8 -0
  173. data/guides/code/getting_started/config/initializers/wrap_parameters.rb +14 -0
  174. data/guides/code/getting_started/config/locales/en.yml +5 -0
  175. data/guides/code/getting_started/config/routes.rb +64 -0
  176. data/guides/code/getting_started/config.ru +4 -0
  177. data/guides/code/getting_started/db/migrate/20110901012504_create_posts.rb +11 -0
  178. data/guides/code/getting_started/db/migrate/20110901012815_create_comments.rb +12 -0
  179. data/guides/code/getting_started/db/migrate/20110901013701_create_tags.rb +11 -0
  180. data/guides/code/getting_started/db/schema.rb +43 -0
  181. data/guides/code/getting_started/db/seeds.rb +7 -0
  182. data/guides/code/getting_started/doc/README_FOR_APP +2 -0
  183. data/guides/code/getting_started/public/404.html +26 -0
  184. data/guides/code/getting_started/public/422.html +26 -0
  185. data/guides/code/getting_started/public/500.html +25 -0
  186. data/guides/code/getting_started/public/favicon.ico +0 -0
  187. data/guides/code/getting_started/public/robots.txt +5 -0
  188. data/guides/code/getting_started/script/rails +6 -0
  189. data/guides/code/getting_started/test/fixtures/comments.yml +11 -0
  190. data/guides/code/getting_started/test/fixtures/posts.yml +11 -0
  191. data/guides/code/getting_started/test/fixtures/tags.yml +9 -0
  192. data/guides/code/getting_started/test/functional/comments_controller_test.rb +7 -0
  193. data/guides/code/getting_started/test/functional/home_controller_test.rb +9 -0
  194. data/guides/code/getting_started/test/functional/posts_controller_test.rb +49 -0
  195. data/guides/code/getting_started/test/performance/browsing_test.rb +12 -0
  196. data/guides/code/getting_started/test/test_helper.rb +13 -0
  197. data/guides/code/getting_started/test/unit/comment_test.rb +7 -0
  198. data/guides/code/getting_started/test/unit/helpers/comments_helper_test.rb +4 -0
  199. data/guides/code/getting_started/test/unit/helpers/home_helper_test.rb +4 -0
  200. data/guides/code/getting_started/test/unit/helpers/posts_helper_test.rb +4 -0
  201. data/guides/code/getting_started/test/unit/post_test.rb +7 -0
  202. data/guides/code/getting_started/test/unit/tag_test.rb +7 -0
  203. data/guides/rails_guides/generator.rb +310 -0
  204. data/guides/rails_guides/helpers.rb +45 -0
  205. data/guides/rails_guides/indexer.rb +69 -0
  206. data/guides/rails_guides/levenshtein.rb +31 -0
  207. data/guides/rails_guides/textile_extensions.rb +63 -0
  208. data/guides/rails_guides.rb +50 -0
  209. data/guides/source/2_2_release_notes.textile +422 -0
  210. data/guides/source/2_3_release_notes.textile +610 -0
  211. data/guides/source/3_0_release_notes.textile +595 -0
  212. data/guides/source/3_1_release_notes.textile +553 -0
  213. data/guides/source/3_2_release_notes.textile +540 -0
  214. data/guides/source/_license.html.erb +2 -0
  215. data/guides/source/_welcome.html.erb +19 -0
  216. data/guides/source/action_controller_overview.textile +820 -0
  217. data/guides/source/action_mailer_basics.textile +516 -0
  218. data/guides/source/action_view_overview.textile +1497 -0
  219. data/guides/source/active_model_basics.textile +205 -0
  220. data/guides/source/active_record_basics.textile +218 -0
  221. data/guides/source/active_record_querying.textile +1433 -0
  222. data/guides/source/active_record_validations_callbacks.textile +1283 -0
  223. data/guides/source/active_resource_basics.textile +120 -0
  224. data/guides/source/active_support_core_extensions.textile +3713 -0
  225. data/guides/source/ajax_on_rails.textile +267 -0
  226. data/guides/source/api_documentation_guidelines.textile +185 -0
  227. data/guides/source/asset_pipeline.textile +707 -0
  228. data/guides/source/association_basics.textile +1959 -0
  229. data/guides/source/caching_with_rails.textile +437 -0
  230. data/guides/source/command_line.textile +574 -0
  231. data/guides/source/configuring.textile +641 -0
  232. data/guides/source/contributing_to_ruby_on_rails.textile +448 -0
  233. data/guides/source/credits.html.erb +72 -0
  234. data/guides/source/debugging_rails_applications.textile +714 -0
  235. data/guides/source/documents.yaml +157 -0
  236. data/guides/source/engines.textile +618 -0
  237. data/guides/source/form_helpers.textile +798 -0
  238. data/guides/source/generators.textile +621 -0
  239. data/guides/source/getting_started.textile +1929 -0
  240. data/guides/source/i18n.textile +931 -0
  241. data/guides/source/index.html.erb +30 -0
  242. data/guides/source/initialization.textile +1116 -0
  243. data/guides/source/kindle/KINDLE.md +26 -0
  244. data/guides/source/kindle/copyright.html.erb +1 -0
  245. data/guides/source/kindle/layout.html.erb +27 -0
  246. data/guides/source/kindle/rails_guides.opf.erb +52 -0
  247. data/guides/source/kindle/toc.html.erb +24 -0
  248. data/guides/source/kindle/toc.ncx.erb +64 -0
  249. data/guides/source/kindle/welcome.html.erb +5 -0
  250. data/guides/source/layout.html.erb +124 -0
  251. data/guides/source/layouts_and_rendering.textile +1237 -0
  252. data/guides/source/migrations.textile +900 -0
  253. data/guides/source/nested_model_forms.textile +222 -0
  254. data/guides/source/performance_testing.textile +597 -0
  255. data/guides/source/plugins.textile +464 -0
  256. data/guides/source/rails_application_templates.textile +240 -0
  257. data/guides/source/rails_on_rack.textile +236 -0
  258. data/guides/source/routing.textile +885 -0
  259. data/guides/source/ruby_on_rails_guides_guidelines.textile +79 -0
  260. data/guides/source/security.textile +1004 -0
  261. data/guides/source/testing.textile +947 -0
  262. data/guides/w3c_validator.rb +91 -0
  263. data/lib/rails/all.rb +15 -0
  264. data/lib/rails/application/bootstrap.rb +73 -0
  265. data/lib/rails/application/configuration.rb +151 -0
  266. data/lib/rails/application/finisher.rb +98 -0
  267. data/lib/rails/application/railties.rb +13 -0
  268. data/lib/rails/application/route_inspector.rb +84 -0
  269. data/lib/rails/application/routes_reloader.rb +56 -0
  270. data/lib/rails/application.rb +320 -0
  271. data/lib/rails/backtrace_cleaner.rb +42 -0
  272. data/lib/rails/cli.rb +16 -0
  273. data/lib/rails/code_statistics.rb +118 -0
  274. data/lib/rails/commands/application.rb +38 -0
  275. data/lib/rails/commands/benchmarker.rb +34 -0
  276. data/lib/rails/commands/console.rb +55 -0
  277. data/lib/rails/commands/dbconsole.rb +123 -0
  278. data/lib/rails/commands/destroy.rb +10 -0
  279. data/lib/rails/commands/generate.rb +12 -0
  280. data/lib/rails/commands/plugin.rb +544 -0
  281. data/lib/rails/commands/plugin_new.rb +11 -0
  282. data/lib/rails/commands/profiler.rb +32 -0
  283. data/lib/rails/commands/runner.rb +54 -0
  284. data/lib/rails/commands/server.rb +100 -0
  285. data/lib/rails/commands/update.rb +9 -0
  286. data/lib/rails/commands.rb +104 -0
  287. data/lib/rails/configuration.rb +89 -0
  288. data/lib/rails/console/app.rb +36 -0
  289. data/lib/rails/console/helpers.rb +11 -0
  290. data/lib/rails/engine/commands.rb +43 -0
  291. data/lib/rails/engine/configuration.rb +84 -0
  292. data/lib/rails/engine/railties.rb +33 -0
  293. data/lib/rails/engine.rb +657 -0
  294. data/lib/rails/generators/actions.rb +325 -0
  295. data/lib/rails/generators/active_model.rb +78 -0
  296. data/lib/rails/generators/app_base.rb +281 -0
  297. data/lib/rails/generators/base.rb +390 -0
  298. data/lib/rails/generators/css/assets/assets_generator.rb +13 -0
  299. data/lib/rails/generators/css/assets/templates/stylesheet.css +4 -0
  300. data/lib/rails/generators/css/scaffold/scaffold_generator.rb +16 -0
  301. data/lib/rails/generators/erb/controller/controller_generator.rb +20 -0
  302. data/lib/rails/generators/erb/controller/templates/view.html.erb +2 -0
  303. data/lib/rails/generators/erb/mailer/mailer_generator.rb +13 -0
  304. data/lib/rails/generators/erb/mailer/templates/view.text.erb +3 -0
  305. data/lib/rails/generators/erb/scaffold/scaffold_generator.rb +29 -0
  306. data/lib/rails/generators/erb/scaffold/templates/_form.html.erb +23 -0
  307. data/lib/rails/generators/erb/scaffold/templates/edit.html.erb +6 -0
  308. data/lib/rails/generators/erb/scaffold/templates/index.html.erb +27 -0
  309. data/lib/rails/generators/erb/scaffold/templates/new.html.erb +5 -0
  310. data/lib/rails/generators/erb/scaffold/templates/show.html.erb +12 -0
  311. data/lib/rails/generators/erb.rb +21 -0
  312. data/lib/rails/generators/generated_attribute.rb +107 -0
  313. data/lib/rails/generators/js/assets/assets_generator.rb +13 -0
  314. data/lib/rails/generators/js/assets/templates/javascript.js +2 -0
  315. data/lib/rails/generators/migration.rb +68 -0
  316. data/lib/rails/generators/named_base.rb +202 -0
  317. data/lib/rails/generators/rails/app/USAGE +15 -0
  318. data/lib/rails/generators/rails/app/app_generator.rb +303 -0
  319. data/lib/rails/generators/rails/app/templates/Gemfile +26 -0
  320. data/lib/rails/generators/rails/app/templates/README +261 -0
  321. data/lib/rails/generators/rails/app/templates/Rakefile +7 -0
  322. data/lib/rails/generators/rails/app/templates/app/assets/images/rails.png +0 -0
  323. data/lib/rails/generators/rails/app/templates/app/assets/javascripts/application.js.tt +17 -0
  324. data/lib/rails/generators/rails/app/templates/app/assets/stylesheets/application.css +13 -0
  325. data/lib/rails/generators/rails/app/templates/app/controllers/application_controller.rb +3 -0
  326. data/lib/rails/generators/rails/app/templates/app/helpers/application_helper.rb +2 -0
  327. data/lib/rails/generators/rails/app/templates/app/mailers/.empty_directory +0 -0
  328. data/lib/rails/generators/rails/app/templates/app/models/.empty_directory +0 -0
  329. data/lib/rails/generators/rails/app/templates/app/views/layouts/application.html.erb.tt +14 -0
  330. data/lib/rails/generators/rails/app/templates/config/application.rb +74 -0
  331. data/lib/rails/generators/rails/app/templates/config/boot.rb +6 -0
  332. data/lib/rails/generators/rails/app/templates/config/databases/frontbase.yml +31 -0
  333. data/lib/rails/generators/rails/app/templates/config/databases/ibm_db.yml +86 -0
  334. data/lib/rails/generators/rails/app/templates/config/databases/jdbc.yml +62 -0
  335. data/lib/rails/generators/rails/app/templates/config/databases/jdbcmysql.yml +33 -0
  336. data/lib/rails/generators/rails/app/templates/config/databases/jdbcpostgresql.yml +43 -0
  337. data/lib/rails/generators/rails/app/templates/config/databases/jdbcsqlite3.yml +20 -0
  338. data/lib/rails/generators/rails/app/templates/config/databases/mysql.yml +54 -0
  339. data/lib/rails/generators/rails/app/templates/config/databases/oracle.yml +39 -0
  340. data/lib/rails/generators/rails/app/templates/config/databases/postgresql.yml +55 -0
  341. data/lib/rails/generators/rails/app/templates/config/databases/sqlite3.yml +25 -0
  342. data/lib/rails/generators/rails/app/templates/config/environment.rb +5 -0
  343. data/lib/rails/generators/rails/app/templates/config/environments/development.rb.tt +41 -0
  344. data/lib/rails/generators/rails/app/templates/config/environments/production.rb.tt +73 -0
  345. data/lib/rails/generators/rails/app/templates/config/environments/test.rb.tt +39 -0
  346. data/lib/rails/generators/rails/app/templates/config/initializers/backtrace_silencers.rb +7 -0
  347. data/lib/rails/generators/rails/app/templates/config/initializers/inflections.rb +15 -0
  348. data/lib/rails/generators/rails/app/templates/config/initializers/mime_types.rb +5 -0
  349. data/lib/rails/generators/rails/app/templates/config/initializers/secret_token.rb.tt +7 -0
  350. data/lib/rails/generators/rails/app/templates/config/initializers/session_store.rb.tt +8 -0
  351. data/lib/rails/generators/rails/app/templates/config/initializers/wrap_parameters.rb.tt +16 -0
  352. data/lib/rails/generators/rails/app/templates/config/locales/en.yml +5 -0
  353. data/lib/rails/generators/rails/app/templates/config/routes.rb +58 -0
  354. data/lib/rails/generators/rails/app/templates/config.ru +4 -0
  355. data/lib/rails/generators/rails/app/templates/db/seeds.rb.tt +7 -0
  356. data/lib/rails/generators/rails/app/templates/doc/README_FOR_APP +2 -0
  357. data/lib/rails/generators/rails/app/templates/gitignore +15 -0
  358. data/lib/rails/generators/rails/app/templates/public/404.html +26 -0
  359. data/lib/rails/generators/rails/app/templates/public/422.html +26 -0
  360. data/lib/rails/generators/rails/app/templates/public/500.html +25 -0
  361. data/lib/rails/generators/rails/app/templates/public/favicon.ico +0 -0
  362. data/lib/rails/generators/rails/app/templates/public/index.html +241 -0
  363. data/lib/rails/generators/rails/app/templates/public/robots.txt +5 -0
  364. data/lib/rails/generators/rails/app/templates/public/stylesheets/.empty_directory +0 -0
  365. data/lib/rails/generators/rails/app/templates/script/rails +5 -0
  366. data/lib/rails/generators/rails/app/templates/test/fixtures/.empty_directory +0 -0
  367. data/lib/rails/generators/rails/app/templates/test/functional/.empty_directory +0 -0
  368. data/lib/rails/generators/rails/app/templates/test/integration/.empty_directory +0 -0
  369. data/lib/rails/generators/rails/app/templates/test/performance/browsing_test.rb +12 -0
  370. data/lib/rails/generators/rails/app/templates/test/test_helper.rb +15 -0
  371. data/lib/rails/generators/rails/app/templates/test/unit/.empty_directory +0 -0
  372. data/lib/rails/generators/rails/assets/USAGE +20 -0
  373. data/lib/rails/generators/rails/assets/assets_generator.rb +25 -0
  374. data/lib/rails/generators/rails/assets/templates/javascript.js +2 -0
  375. data/lib/rails/generators/rails/assets/templates/stylesheet.css +4 -0
  376. data/lib/rails/generators/rails/controller/USAGE +18 -0
  377. data/lib/rails/generators/rails/controller/controller_generator.rb +20 -0
  378. data/lib/rails/generators/rails/controller/templates/controller.rb +13 -0
  379. data/lib/rails/generators/rails/generator/USAGE +12 -0
  380. data/lib/rails/generators/rails/generator/generator_generator.rb +25 -0
  381. data/lib/rails/generators/rails/generator/templates/%file_name%_generator.rb.tt +3 -0
  382. data/lib/rails/generators/rails/generator/templates/USAGE.tt +8 -0
  383. data/lib/rails/generators/rails/generator/templates/templates/.empty_directory +0 -0
  384. data/lib/rails/generators/rails/helper/USAGE +17 -0
  385. data/lib/rails/generators/rails/helper/helper_generator.rb +13 -0
  386. data/lib/rails/generators/rails/helper/templates/helper.rb +4 -0
  387. data/lib/rails/generators/rails/integration_test/USAGE +10 -0
  388. data/lib/rails/generators/rails/integration_test/integration_test_generator.rb +7 -0
  389. data/lib/rails/generators/rails/migration/USAGE +29 -0
  390. data/lib/rails/generators/rails/migration/migration_generator.rb +8 -0
  391. data/lib/rails/generators/rails/model/USAGE +45 -0
  392. data/lib/rails/generators/rails/model/model_generator.rb +8 -0
  393. data/lib/rails/generators/rails/observer/USAGE +12 -0
  394. data/lib/rails/generators/rails/observer/observer_generator.rb +7 -0
  395. data/lib/rails/generators/rails/performance_test/USAGE +10 -0
  396. data/lib/rails/generators/rails/performance_test/performance_test_generator.rb +7 -0
  397. data/lib/rails/generators/rails/plugin_new/USAGE +10 -0
  398. data/lib/rails/generators/rails/plugin_new/plugin_new_generator.rb +318 -0
  399. data/lib/rails/generators/rails/plugin_new/templates/%name%.gemspec +29 -0
  400. data/lib/rails/generators/rails/plugin_new/templates/Gemfile +23 -0
  401. data/lib/rails/generators/rails/plugin_new/templates/MIT-LICENSE +20 -0
  402. data/lib/rails/generators/rails/plugin_new/templates/README.rdoc +3 -0
  403. data/lib/rails/generators/rails/plugin_new/templates/Rakefile +31 -0
  404. data/lib/rails/generators/rails/plugin_new/templates/app/controllers/%name%/application_controller.rb.tt +4 -0
  405. data/lib/rails/generators/rails/plugin_new/templates/app/helpers/%name%/application_helper.rb.tt +4 -0
  406. data/lib/rails/generators/rails/plugin_new/templates/app/mailers/.empty_directory +0 -0
  407. data/lib/rails/generators/rails/plugin_new/templates/app/models/.empty_directory +0 -0
  408. data/lib/rails/generators/rails/plugin_new/templates/app/views/layouts/%name%/application.html.erb.tt +14 -0
  409. data/lib/rails/generators/rails/plugin_new/templates/config/routes.rb +6 -0
  410. data/lib/rails/generators/rails/plugin_new/templates/gitignore +7 -0
  411. data/lib/rails/generators/rails/plugin_new/templates/lib/%name%/engine.rb +7 -0
  412. data/lib/rails/generators/rails/plugin_new/templates/lib/%name%/version.rb +3 -0
  413. data/lib/rails/generators/rails/plugin_new/templates/lib/%name%.rb +6 -0
  414. data/lib/rails/generators/rails/plugin_new/templates/lib/tasks/%name%_tasks.rake +4 -0
  415. data/lib/rails/generators/rails/plugin_new/templates/rails/application.rb +18 -0
  416. data/lib/rails/generators/rails/plugin_new/templates/rails/boot.rb +10 -0
  417. data/lib/rails/generators/rails/plugin_new/templates/rails/routes.rb +4 -0
  418. data/lib/rails/generators/rails/plugin_new/templates/script/rails.tt +7 -0
  419. data/lib/rails/generators/rails/plugin_new/templates/test/%name%_test.rb +7 -0
  420. data/lib/rails/generators/rails/plugin_new/templates/test/integration/navigation_test.rb +12 -0
  421. data/lib/rails/generators/rails/plugin_new/templates/test/test_helper.rb +15 -0
  422. data/lib/rails/generators/rails/resource/USAGE +23 -0
  423. data/lib/rails/generators/rails/resource/resource_generator.rb +20 -0
  424. data/lib/rails/generators/rails/resource_route/resource_route_generator.rb +50 -0
  425. data/lib/rails/generators/rails/scaffold/USAGE +35 -0
  426. data/lib/rails/generators/rails/scaffold/scaffold_generator.rb +23 -0
  427. data/lib/rails/generators/rails/scaffold/templates/scaffold.css +56 -0
  428. data/lib/rails/generators/rails/scaffold_controller/USAGE +19 -0
  429. data/lib/rails/generators/rails/scaffold_controller/scaffold_controller_generator.rb +25 -0
  430. data/lib/rails/generators/rails/scaffold_controller/templates/controller.rb +89 -0
  431. data/lib/rails/generators/rails/session_migration/USAGE +8 -0
  432. data/lib/rails/generators/rails/session_migration/session_migration_generator.rb +8 -0
  433. data/lib/rails/generators/rails/task/USAGE +9 -0
  434. data/lib/rails/generators/rails/task/task_generator.rb +12 -0
  435. data/lib/rails/generators/rails/task/templates/task.rb +8 -0
  436. data/lib/rails/generators/resource_helpers.rb +80 -0
  437. data/lib/rails/generators/test_case.rb +247 -0
  438. data/lib/rails/generators/test_unit/controller/controller_generator.rb +15 -0
  439. data/lib/rails/generators/test_unit/controller/templates/functional_test.rb +19 -0
  440. data/lib/rails/generators/test_unit/helper/helper_generator.rb +13 -0
  441. data/lib/rails/generators/test_unit/helper/templates/helper_test.rb +6 -0
  442. data/lib/rails/generators/test_unit/integration/integration_generator.rb +13 -0
  443. data/lib/rails/generators/test_unit/integration/templates/integration_test.rb +7 -0
  444. data/lib/rails/generators/test_unit/mailer/mailer_generator.rb +14 -0
  445. data/lib/rails/generators/test_unit/mailer/templates/functional_test.rb +21 -0
  446. data/lib/rails/generators/test_unit/model/model_generator.rb +24 -0
  447. data/lib/rails/generators/test_unit/model/templates/fixtures.yml +23 -0
  448. data/lib/rails/generators/test_unit/model/templates/unit_test.rb +9 -0
  449. data/lib/rails/generators/test_unit/observer/observer_generator.rb +13 -0
  450. data/lib/rails/generators/test_unit/observer/templates/unit_test.rb +9 -0
  451. data/lib/rails/generators/test_unit/performance/performance_generator.rb +13 -0
  452. data/lib/rails/generators/test_unit/performance/templates/performance_test.rb +12 -0
  453. data/lib/rails/generators/test_unit/plugin/plugin_generator.rb +13 -0
  454. data/lib/rails/generators/test_unit/plugin/templates/%file_name%_test.rb.tt +7 -0
  455. data/lib/rails/generators/test_unit/plugin/templates/test_helper.rb +3 -0
  456. data/lib/rails/generators/test_unit/scaffold/scaffold_generator.rb +38 -0
  457. data/lib/rails/generators/test_unit/scaffold/templates/functional_test.rb +51 -0
  458. data/lib/rails/generators/test_unit.rb +8 -0
  459. data/lib/rails/generators.rb +339 -0
  460. data/lib/rails/info.rb +116 -0
  461. data/lib/rails/info_controller.rb +15 -0
  462. data/lib/rails/initializable.rb +89 -0
  463. data/lib/rails/paths.rb +211 -0
  464. data/lib/rails/performance_test_help.rb +3 -0
  465. data/lib/rails/plugin.rb +92 -0
  466. data/lib/rails/rack/debugger.rb +24 -0
  467. data/lib/rails/rack/log_tailer.rb +34 -0
  468. data/lib/rails/rack/logger.rb +60 -0
  469. data/lib/rails/rack.rb +7 -0
  470. data/lib/rails/railtie/configurable.rb +35 -0
  471. data/lib/rails/railtie/configuration.rb +90 -0
  472. data/lib/rails/railtie.rb +202 -0
  473. data/lib/rails/ruby_version_check.rb +23 -0
  474. data/lib/rails/rubyprof_ext.rb +35 -0
  475. data/lib/rails/script_rails_loader.rb +29 -0
  476. data/lib/rails/source_annotation_extractor.rb +104 -0
  477. data/lib/rails/tasks/annotations.rake +20 -0
  478. data/lib/rails/tasks/documentation.rake +155 -0
  479. data/lib/rails/tasks/engine.rake +72 -0
  480. data/lib/rails/tasks/framework.rake +76 -0
  481. data/lib/rails/tasks/log.rake +9 -0
  482. data/lib/rails/tasks/middleware.rake +7 -0
  483. data/lib/rails/tasks/misc.rake +67 -0
  484. data/lib/rails/tasks/routes.rake +9 -0
  485. data/lib/rails/tasks/statistics.rake +16 -0
  486. data/lib/rails/tasks/tmp.rake +37 -0
  487. data/lib/rails/tasks.rb +16 -0
  488. data/lib/rails/test_help.rb +56 -0
  489. data/lib/rails/test_unit/railtie.rb +15 -0
  490. data/lib/rails/test_unit/sub_test_task.rb +8 -0
  491. data/lib/rails/test_unit/testing.rake +146 -0
  492. data/lib/rails/version.rb +10 -0
  493. data/lib/rails.rb +126 -0
  494. metadata +609 -0
@@ -0,0 +1,947 @@
1
+ h2. A Guide to Testing Rails Applications
2
+
3
+ This guide covers built-in mechanisms offered by Rails to test your application. By referring to this guide, you will be able to:
4
+
5
+ * Understand Rails testing terminology
6
+ * Write unit, functional and integration tests for your application
7
+ * Identify other popular testing approaches and plugins
8
+
9
+ This guide won't teach you to write a Rails application; it assumes basic familiarity with the Rails way of doing things.
10
+
11
+ endprologue.
12
+
13
+ h3. Why Write Tests for your Rails Applications?
14
+
15
+ * Rails makes it super easy to write your tests. It starts by producing skeleton test code in the background while you are creating your models and controllers.
16
+ * By simply running your Rails tests you can ensure your code adheres to the desired functionality even after some major code refactoring.
17
+ * Rails tests can also simulate browser requests and thus you can test your application's response without having to test it through your browser.
18
+
19
+ h3. Introduction to Testing
20
+
21
+ Testing support was woven into the Rails fabric from the beginning. It wasn't an "oh! let's bolt on support for running tests because they're new and cool" epiphany. Just about every Rails application interacts heavily with a database - and, as a result, your tests will need a database to interact with as well. To write efficient tests, you'll need to understand how to set up this database and populate it with sample data.
22
+
23
+ h4. The Three Environments
24
+
25
+ Every Rails application you build has 3 sides: a side for production, a side for development, and a side for testing.
26
+
27
+ One place you'll find this distinction is in the +config/database.yml+ file. This YAML configuration file has 3 different sections defining 3 unique database setups:
28
+
29
+ * production
30
+ * development
31
+ * test
32
+
33
+ This allows you to set up and interact with test data without any danger of your tests altering data from your production environment.
34
+
35
+ For example, suppose you need to test your new +delete_this_user_and_every_everything_associated_with_it+ function. Wouldn't you want to run this in an environment where it makes no difference if you destroy data or not?
36
+
37
+ When you do end up destroying your testing database (and it will happen, trust me), you can rebuild it from scratch according to the specs defined in the development database. You can do this by running +rake db:test:prepare+.
38
+
39
+ h4. Rails Sets up for Testing from the Word Go
40
+
41
+ Rails creates a +test+ folder for you as soon as you create a Rails project using +rails new+ _application_name_. If you list the contents of this folder then you shall see:
42
+
43
+ <shell>
44
+ $ ls -F test/
45
+
46
+ fixtures/ functional/ integration/ test_helper.rb unit/
47
+ </shell>
48
+
49
+ The +unit+ folder is meant to hold tests for your models, the +functional+ folder is meant to hold tests for your controllers, and the +integration+ folder is meant to hold tests that involve any number of controllers interacting. Fixtures are a way of organizing test data; they reside in the +fixtures+ folder. The +test_helper.rb+ file holds the default configuration for your tests.
50
+
51
+ h4. The Low-Down on Fixtures
52
+
53
+ For good tests, you'll need to give some thought to setting up test data. In Rails, you can handle this by defining and customizing fixtures.
54
+
55
+ h5. What are Fixtures?
56
+
57
+ _Fixtures_ is a fancy word for sample data. Fixtures allow you to populate your testing database with predefined data before your tests run. Fixtures are database independent and assume a single format: *YAML*.
58
+
59
+ You'll find fixtures under your +test/fixtures+ directory. When you run +rails generate model+ to create a new model, fixture stubs will be automatically created and placed in this directory.
60
+
61
+ h5. YAML
62
+
63
+ YAML-formatted fixtures are a very human-friendly way to describe your sample data. These types of fixtures have the *.yml* file extension (as in +users.yml+).
64
+
65
+ Here's a sample YAML fixture file:
66
+
67
+ <yaml>
68
+ # lo & behold! I am a YAML comment!
69
+ david:
70
+ name: David Heinemeier Hansson
71
+ birthday: 1979-10-15
72
+ profession: Systems development
73
+
74
+ steve:
75
+ name: Steve Ross Kellock
76
+ birthday: 1974-09-27
77
+ profession: guy with keyboard
78
+ </yaml>
79
+
80
+ Each fixture is given a name followed by an indented list of colon-separated key/value pairs. Records are separated by a blank space. You can place comments in a fixture file by using the # character in the first column.
81
+
82
+ h5. ERB'in It Up
83
+
84
+ ERB allows you to embed ruby code within templates. YAML fixture format is pre-processed with ERB when you load fixtures. This allows you to use Ruby to help you generate some sample data.
85
+
86
+ <erb>
87
+ <% earth_size = 20 %>
88
+ mercury:
89
+ size: <%= earth_size / 50 %>
90
+ brightest_on: <%= 113.days.ago.to_s(:db) %>
91
+
92
+ venus:
93
+ size: <%= earth_size / 2 %>
94
+ brightest_on: <%= 67.days.ago.to_s(:db) %>
95
+
96
+ mars:
97
+ size: <%= earth_size - 69 %>
98
+ brightest_on: <%= 13.days.from_now.to_s(:db) %>
99
+ </erb>
100
+
101
+ Anything encased within the
102
+
103
+ <erb>
104
+ <% %>
105
+ </erb>
106
+
107
+ tag is considered Ruby code. When this fixture is loaded, the +size+ attribute of the three records will be set to 20/50, 20/2, and 20-69 respectively. The +brightest_on+ attribute will also be evaluated and formatted by Rails to be compatible with the database.
108
+
109
+ h5. Fixtures in Action
110
+
111
+ Rails by default automatically loads all fixtures from the +test/fixtures+ folder for your unit and functional test. Loading involves three steps:
112
+
113
+ * Remove any existing data from the table corresponding to the fixture
114
+ * Load the fixture data into the table
115
+ * Dump the fixture data into a variable in case you want to access it directly
116
+
117
+ h5. Hashes with Special Powers
118
+
119
+ Fixtures are basically Hash objects. As mentioned in point #3 above, you can access the hash object directly because it is automatically setup as a local variable of the test case. For example:
120
+
121
+ <ruby>
122
+ # this will return the Hash for the fixture named david
123
+ users(:david)
124
+
125
+ # this will return the property for david called id
126
+ users(:david).id
127
+ </ruby>
128
+
129
+ Fixtures can also transform themselves into the form of the original class. Thus, you can get at the methods only available to that class.
130
+
131
+ <ruby>
132
+ # using the find method, we grab the "real" david as a User
133
+ david = users(:david).find
134
+
135
+ # and now we have access to methods only available to a User class
136
+ email(david.girlfriend.email, david.location_tonight)
137
+ </ruby>
138
+
139
+ h3. Unit Testing your Models
140
+
141
+ In Rails, unit tests are what you write to test your models.
142
+
143
+ For this guide we will be using Rails _scaffolding_. It will create the model, a migration, controller and views for the new resource in a single operation. It will also create a full test suite following Rails best practices. I will be using examples from this generated code and will be supplementing it with additional examples where necessary.
144
+
145
+ NOTE: For more information on Rails <i>scaffolding</i>, refer to "Getting Started with Rails":getting_started.html
146
+
147
+ When you use +rails generate scaffold+, for a resource among other things it creates a test stub in the +test/unit+ folder:
148
+
149
+ <shell>
150
+ $ rails generate scaffold post title:string body:text
151
+ ...
152
+ create app/models/post.rb
153
+ create test/unit/post_test.rb
154
+ create test/fixtures/posts.yml
155
+ ...
156
+ </shell>
157
+
158
+ The default test stub in +test/unit/post_test.rb+ looks like this:
159
+
160
+ <ruby>
161
+ require 'test_helper'
162
+
163
+ class PostTest < ActiveSupport::TestCase
164
+ # Replace this with your real tests.
165
+ test "the truth" do
166
+ assert true
167
+ end
168
+ end
169
+ </ruby>
170
+
171
+ A line by line examination of this file will help get you oriented to Rails testing code and terminology.
172
+
173
+ <ruby>
174
+ require 'test_helper'
175
+ </ruby>
176
+
177
+ As you know by now, +test_helper.rb+ specifies the default configuration to run our tests. This is included with all the tests, so any methods added to this file are available to all your tests.
178
+
179
+ <ruby>
180
+ class PostTest < ActiveSupport::TestCase
181
+ </ruby>
182
+
183
+ The +PostTest+ class defines a _test case_ because it inherits from +ActiveSupport::TestCase+. +PostTest+ thus has all the methods available from +ActiveSupport::TestCase+. You'll see those methods a little later in this guide.
184
+
185
+ Any method defined within a +Test::Unit+ test case that begins with +test+ (case sensitive) is simply called a test. So, +test_password+, +test_valid_password+ and +testValidPassword+ all are legal test names and are run automatically when the test case is run.
186
+
187
+ Rails adds a +test+ method that takes a test name and a block. It generates a normal +Test::Unit+ test with method names prefixed with +test_+. So,
188
+
189
+ <ruby>
190
+ test "the truth" do
191
+ assert true
192
+ end
193
+ </ruby>
194
+
195
+ acts as if you had written
196
+
197
+ <ruby>
198
+ def test_the_truth
199
+ assert true
200
+ end
201
+ </ruby>
202
+
203
+ only the +test+ macro allows a more readable test name. You can still use regular method definitions though.
204
+
205
+ NOTE: The method name is generated by replacing spaces with underscores. The result does not need to be a valid Ruby identifier though, the name may contain punctuation characters etc. That's because in Ruby technically any string may be a method name. Odd ones need +define_method+ and +send+ calls, but formally there's no restriction.
206
+
207
+ <ruby>
208
+ assert true
209
+ </ruby>
210
+
211
+ This line of code is called an _assertion_. An assertion is a line of code that evaluates an object (or expression) for expected results. For example, an assertion can check:
212
+
213
+ * does this value = that value?
214
+ * is this object nil?
215
+ * does this line of code throw an exception?
216
+ * is the user's password greater than 5 characters?
217
+
218
+ Every test contains one or more assertions. Only when all the assertions are successful will the test pass.
219
+
220
+ h4. Preparing your Application for Testing
221
+
222
+ Before you can run your tests, you need to ensure that the test database structure is current. For this you can use the following rake commands:
223
+
224
+ <shell>
225
+ $ rake db:migrate
226
+ ...
227
+ $ rake db:test:load
228
+ </shell>
229
+
230
+ The +rake db:migrate+ above runs any pending migrations on the _development_ environment and updates +db/schema.rb+. The +rake db:test:load+ recreates the test database from the current +db/schema.rb+. On subsequent attempts, it is a good idea to first run +db:test:prepare+, as it first checks for pending migrations and warns you appropriately.
231
+
232
+ NOTE: +db:test:prepare+ will fail with an error if +db/schema.rb+ doesn't exist.
233
+
234
+ h5. Rake Tasks for Preparing your Application for Testing
235
+
236
+ |_.Tasks |_.Description|
237
+ |+rake db:test:clone+ |Recreate the test database from the current environment's database schema|
238
+ |+rake db:test:clone_structure+ |Recreate the test database from the development structure|
239
+ |+rake db:test:load+ |Recreate the test database from the current +schema.rb+|
240
+ |+rake db:test:prepare+ |Check for pending migrations and load the test schema|
241
+ |+rake db:test:purge+ |Empty the test database.|
242
+
243
+ TIP: You can see all these rake tasks and their descriptions by running +rake --tasks --describe+
244
+
245
+ h4. Running Tests
246
+
247
+ Running a test is as simple as invoking the file containing the test cases through Ruby:
248
+
249
+ <shell>
250
+ $ ruby -Itest test/unit/post_test.rb
251
+
252
+ Loaded suite unit/post_test
253
+ Started
254
+ .
255
+ Finished in 0.023513 seconds.
256
+
257
+ 1 tests, 1 assertions, 0 failures, 0 errors
258
+ </shell>
259
+
260
+ This will run all the test methods from the test case. Note that +test_helper.rb+ is in the +test+ directory, hence this directory needs to be added to the load path using the +-I+ switch.
261
+
262
+ You can also run a particular test method from the test case by using the +-n+ switch with the +test method name+.
263
+
264
+ <shell>
265
+ $ ruby -Itest test/unit/post_test.rb -n test_the_truth
266
+
267
+ Loaded suite unit/post_test
268
+ Started
269
+ .
270
+ Finished in 0.023513 seconds.
271
+
272
+ 1 tests, 1 assertions, 0 failures, 0 errors
273
+ </shell>
274
+
275
+ The +.+ (dot) above indicates a passing test. When a test fails you see an +F+; when a test throws an error you see an +E+ in its place. The last line of the output is the summary.
276
+
277
+ To see how a test failure is reported, you can add a failing test to the +post_test.rb+ test case.
278
+
279
+ <ruby>
280
+ test "should not save post without title" do
281
+ post = Post.new
282
+ assert !post.save
283
+ end
284
+ </ruby>
285
+
286
+ Let us run this newly added test.
287
+
288
+ <shell>
289
+ $ ruby unit/post_test.rb -n test_should_not_save_post_without_title
290
+ Loaded suite -e
291
+ Started
292
+ F
293
+ Finished in 0.102072 seconds.
294
+
295
+ 1) Failure:
296
+ test_should_not_save_post_without_title(PostTest) [/test/unit/post_test.rb:6]:
297
+ <false> is not true.
298
+
299
+ 1 tests, 1 assertions, 1 failures, 0 errors
300
+ </shell>
301
+
302
+ In the output, +F+ denotes a failure. You can see the corresponding trace shown under +1)+ along with the name of the failing test. The next few lines contain the stack trace followed by a message which mentions the actual value and the expected value by the assertion. The default assertion messages provide just enough information to help pinpoint the error. To make the assertion failure message more readable, every assertion provides an optional message parameter, as shown here:
303
+
304
+ <ruby>
305
+ test "should not save post without title" do
306
+ post = Post.new
307
+ assert !post.save, "Saved the post without a title"
308
+ end
309
+ </ruby>
310
+
311
+ Running this test shows the friendlier assertion message:
312
+
313
+ <shell>
314
+ 1) Failure:
315
+ test_should_not_save_post_without_title(PostTest) [/test/unit/post_test.rb:6]:
316
+ Saved the post without a title.
317
+ <false> is not true.
318
+ </shell>
319
+
320
+ Now to get this test to pass we can add a model level validation for the _title_ field.
321
+
322
+ <ruby>
323
+ class Post < ActiveRecord::Base
324
+ validates :title, :presence => true
325
+ end
326
+ </ruby>
327
+
328
+ Now the test should pass. Let us verify by running the test again:
329
+
330
+ <shell>
331
+ $ ruby unit/post_test.rb -n test_should_not_save_post_without_title
332
+ Loaded suite unit/post_test
333
+ Started
334
+ .
335
+ Finished in 0.193608 seconds.
336
+
337
+ 1 tests, 1 assertions, 0 failures, 0 errors
338
+ </shell>
339
+
340
+ Now, if you noticed, we first wrote a test which fails for a desired functionality, then we wrote some code which adds the functionality and finally we ensured that our test passes. This approach to software development is referred to as _Test-Driven Development_ (TDD).
341
+
342
+ TIP: Many Rails developers practice _Test-Driven Development_ (TDD). This is an excellent way to build up a test suite that exercises every part of your application. TDD is beyond the scope of this guide, but one place to start is with "15 TDD steps to create a Rails application":http://andrzejonsoftware.blogspot.com/2007/05/15-tdd-steps-to-create-rails.html.
343
+
344
+ To see how an error gets reported, here's a test containing an error:
345
+
346
+ <ruby>
347
+ test "should report error" do
348
+ # some_undefined_variable is not defined elsewhere in the test case
349
+ some_undefined_variable
350
+ assert true
351
+ end
352
+ </ruby>
353
+
354
+ Now you can see even more output in the console from running the tests:
355
+
356
+ <shell>
357
+ $ ruby unit/post_test.rb -n test_should_report_error
358
+ Loaded suite -e
359
+ Started
360
+ E
361
+ Finished in 0.082603 seconds.
362
+
363
+ 1) Error:
364
+ test_should_report_error(PostTest):
365
+ NameError: undefined local variable or method `some_undefined_variable' for #<PostTest:0x249d354>
366
+ /test/unit/post_test.rb:6:in `test_should_report_error'
367
+
368
+ 1 tests, 0 assertions, 0 failures, 1 errors
369
+ </shell>
370
+
371
+ Notice the 'E' in the output. It denotes a test with error.
372
+
373
+ NOTE: The execution of each test method stops as soon as any error or an assertion failure is encountered, and the test suite continues with the next method. All test methods are executed in alphabetical order.
374
+
375
+ h4. What to Include in Your Unit Tests
376
+
377
+ Ideally, you would like to include a test for everything which could possibly break. It's a good practice to have at least one test for each of your validations and at least one test for every method in your model.
378
+
379
+ h4. Assertions Available
380
+
381
+ By now you've caught a glimpse of some of the assertions that are available. Assertions are the worker bees of testing. They are the ones that actually perform the checks to ensure that things are going as planned.
382
+
383
+ There are a bunch of different types of assertions you can use. Here's the complete list of assertions that ship with +test/unit+, the default testing library used by Rails. The +[msg]+ parameter is an optional string message you can specify to make your test failure messages clearer. It's not required.
384
+
385
+ |_.Assertion |_.Purpose|
386
+ |+assert( boolean, [msg] )+ |Ensures that the object/expression is true.|
387
+ |+assert_equal( obj1, obj2, [msg] )+ |Ensures that +obj1 == obj2+ is true.|
388
+ |+assert_not_equal( obj1, obj2, [msg] )+ |Ensures that +obj1 == obj2+ is false.|
389
+ |+assert_same( obj1, obj2, [msg] )+ |Ensures that +obj1.equal?(obj2)+ is true.|
390
+ |+assert_not_same( obj1, obj2, [msg] )+ |Ensures that +obj1.equal?(obj2)+ is false.|
391
+ |+assert_nil( obj, [msg] )+ |Ensures that +obj.nil?+ is true.|
392
+ |+assert_not_nil( obj, [msg] )+ |Ensures that +obj.nil?+ is false.|
393
+ |+assert_match( regexp, string, [msg] )+ |Ensures that a string matches the regular expression.|
394
+ |+assert_no_match( regexp, string, [msg] )+ |Ensures that a string doesn't match the regular expression.|
395
+ |+assert_in_delta( expecting, actual, delta, [msg] )+ |Ensures that the numbers +expecting+ and +actual+ are within +delta+ of each other.|
396
+ |+assert_throws( symbol, [msg] ) { block }+ |Ensures that the given block throws the symbol.|
397
+ |+assert_raise( exception1, exception2, ... ) { block }+ |Ensures that the given block raises one of the given exceptions.|
398
+ |+assert_nothing_raised( exception1, exception2, ... ) { block }+ |Ensures that the given block doesn't raise one of the given exceptions.|
399
+ |+assert_instance_of( class, obj, [msg] )+ |Ensures that +obj+ is of the +class+ type.|
400
+ |+assert_kind_of( class, obj, [msg] )+ |Ensures that +obj+ is or descends from +class+.|
401
+ |+assert_respond_to( obj, symbol, [msg] )+ |Ensures that +obj+ has a method called +symbol+.|
402
+ |+assert_operator( obj1, operator, obj2, [msg] )+ |Ensures that +obj1.operator(obj2)+ is true.|
403
+ |+assert_send( array, [msg] )+ |Ensures that executing the method listed in +array[1]+ on the object in +array[0]+ with the parameters of +array[2 and up]+ is true. This one is weird eh?|
404
+ |+flunk( [msg] )+ |Ensures failure. This is useful to explicitly mark a test that isn't finished yet.|
405
+
406
+ Because of the modular nature of the testing framework, it is possible to create your own assertions. In fact, that's exactly what Rails does. It includes some specialized assertions to make your life easier.
407
+
408
+ NOTE: Creating your own assertions is an advanced topic that we won't cover in this tutorial.
409
+
410
+ h4. Rails Specific Assertions
411
+
412
+ Rails adds some custom assertions of its own to the +test/unit+ framework:
413
+
414
+ NOTE: +assert_valid(record)+ has been deprecated. Please use +assert(record.valid?)+ instead.
415
+
416
+ |_.Assertion |_.Purpose|
417
+ |+assert_valid(record)+ |Ensures that the passed record is valid by Active Record standards and returns any error messages if it is not.|
418
+ |+assert_difference(expressions, difference = 1, message = nil) {...}+ |Test numeric difference between the return value of an expression as a result of what is evaluated in the yielded block.|
419
+ |+assert_no_difference(expressions, message = nil, &amp;block)+ |Asserts that the numeric result of evaluating an expression is not changed before and after invoking the passed in block.|
420
+ |+assert_recognizes(expected_options, path, extras={}, message=nil)+ |Asserts that the routing of the given path was handled correctly and that the parsed options (given in the expected_options hash) match path. Basically, it asserts that Rails recognizes the route given by expected_options.|
421
+ |+assert_generates(expected_path, options, defaults={}, extras = {}, message=nil)+ |Asserts that the provided options can be used to generate the provided path. This is the inverse of assert_recognizes. The extras parameter is used to tell the request the names and values of additional request parameters that would be in a query string. The message parameter allows you to specify a custom error message for assertion failures.|
422
+ |+assert_response(type, message = nil)+ |Asserts that the response comes with a specific status code. You can specify +:success+ to indicate 200, +:redirect+ to indicate 300-399, +:missing+ to indicate 404, or +:error+ to match the 500-599 range|
423
+ |+assert_redirected_to(options = {}, message=nil)+ |Assert that the redirection options passed in match those of the redirect called in the latest action. This match can be partial, such that +assert_redirected_to(:controller => "weblog")+ will also match the redirection of +redirect_to(:controller => "weblog", :action => "show")+ and so on.|
424
+ |+assert_template(expected = nil, message=nil)+ |Asserts that the request was rendered with the appropriate template file.|
425
+
426
+ You'll see the usage of some of these assertions in the next chapter.
427
+
428
+ h3. Functional Tests for Your Controllers
429
+
430
+ In Rails, testing the various actions of a single controller is called writing functional tests for that controller. Controllers handle the incoming web requests to your application and eventually respond with a rendered view.
431
+
432
+ h4. What to Include in your Functional Tests
433
+
434
+ You should test for things such as:
435
+
436
+ * was the web request successful?
437
+ * was the user redirected to the right page?
438
+ * was the user successfully authenticated?
439
+ * was the correct object stored in the response template?
440
+ * was the appropriate message displayed to the user in the view?
441
+
442
+ Now that we have used Rails scaffold generator for our +Post+ resource, it has already created the controller code and functional tests. You can take look at the file +posts_controller_test.rb+ in the +test/functional+ directory.
443
+
444
+ Let me take you through one such test, +test_should_get_index+ from the file +posts_controller_test.rb+.
445
+
446
+ <ruby>
447
+ test "should get index" do
448
+ get :index
449
+ assert_response :success
450
+ assert_not_nil assigns(:posts)
451
+ end
452
+ </ruby>
453
+
454
+ In the +test_should_get_index+ test, Rails simulates a request on the action called +index+, making sure the request was successful and also ensuring that it assigns a valid +posts+ instance variable.
455
+
456
+ The +get+ method kicks off the web request and populates the results into the response. It accepts 4 arguments:
457
+
458
+ * The action of the controller you are requesting. This can be in the form of a string or a symbol.
459
+ * An optional hash of request parameters to pass into the action (eg. query string parameters or post variables).
460
+ * An optional hash of session variables to pass along with the request.
461
+ * An optional hash of flash values.
462
+
463
+ Example: Calling the +:show+ action, passing an +id+ of 12 as the +params+ and setting a +user_id+ of 5 in the session:
464
+
465
+ <ruby>
466
+ get(:show, {'id' => "12"}, {'user_id' => 5})
467
+ </ruby>
468
+
469
+ Another example: Calling the +:view+ action, passing an +id+ of 12 as the +params+, this time with no session, but with a flash message.
470
+
471
+ <ruby>
472
+ get(:view, {'id' => '12'}, nil, {'message' => 'booya!'})
473
+ </ruby>
474
+
475
+ NOTE: If you try running +test_should_create_post+ test from +posts_controller_test.rb+ it will fail on account of the newly added model level validation and rightly so.
476
+
477
+ Let us modify +test_should_create_post+ test in +posts_controller_test.rb+ so that all our test pass:
478
+
479
+ <ruby>
480
+ test "should create post" do
481
+ assert_difference('Post.count') do
482
+ post :create, :post => { :title => 'Some title'}
483
+ end
484
+
485
+ assert_redirected_to post_path(assigns(:post))
486
+ end
487
+ </ruby>
488
+
489
+ Now you can try running all the tests and they should pass.
490
+
491
+ h4. Available Request Types for Functional Tests
492
+
493
+ If you're familiar with the HTTP protocol, you'll know that +get+ is a type of request. There are 5 request types supported in Rails functional tests:
494
+
495
+ * +get+
496
+ * +post+
497
+ * +put+
498
+ * +head+
499
+ * +delete+
500
+
501
+ All of request types are methods that you can use, however, you'll probably end up using the first two more often than the others.
502
+
503
+ NOTE: Functional tests do not verify whether the specified request type should be accepted by the action. Request types in this context exist to make your tests more descriptive.
504
+
505
+ h4. The Four Hashes of the Apocalypse
506
+
507
+ After a request has been made by using one of the 5 methods (+get+, +post+, etc.) and processed, you will have 4 Hash objects ready for use:
508
+
509
+ * +assigns+ - Any objects that are stored as instance variables in actions for use in views.
510
+ * +cookies+ - Any cookies that are set.
511
+ * +flash+ - Any objects living in the flash.
512
+ * +session+ - Any object living in session variables.
513
+
514
+ As is the case with normal Hash objects, you can access the values by referencing the keys by string. You can also reference them by symbol name, except for +assigns+. For example:
515
+
516
+ <ruby>
517
+ flash["gordon"] flash[:gordon]
518
+ session["shmession"] session[:shmession]
519
+ cookies["are_good_for_u"] cookies[:are_good_for_u]
520
+
521
+ # Because you can't use assigns[:something] for historical reasons:
522
+ assigns["something"] assigns(:something)
523
+ </ruby>
524
+
525
+ h4. Instance Variables Available
526
+
527
+ You also have access to three instance variables in your functional tests:
528
+
529
+ * +@controller+ - The controller processing the request
530
+ * +@request+ - The request
531
+ * +@response+ - The response
532
+
533
+ h4. A Fuller Functional Test Example
534
+
535
+ Here's another example that uses +flash+, +assert_redirected_to+, and +assert_difference+:
536
+
537
+ <ruby>
538
+ test "should create post" do
539
+ assert_difference('Post.count') do
540
+ post :create, :post => { :title => 'Hi', :body => 'This is my first post.'}
541
+ end
542
+ assert_redirected_to post_path(assigns(:post))
543
+ assert_equal 'Post was successfully created.', flash[:notice]
544
+ end
545
+ </ruby>
546
+
547
+ h4. Testing Views
548
+
549
+ Testing the response to your request by asserting the presence of key HTML elements and their content is a useful way to test the views of your application. The +assert_select+ assertion allows you to do this by using a simple yet powerful syntax.
550
+
551
+ NOTE: You may find references to +assert_tag+ in other documentation, but this is now deprecated in favor of +assert_select+.
552
+
553
+ There are two forms of +assert_select+:
554
+
555
+ +assert_select(selector, [equality], [message])+ ensures that the equality condition is met on the selected elements through the selector. The selector may be a CSS selector expression (String), an expression with substitution values, or an +HTML::Selector+ object.
556
+
557
+ +assert_select(element, selector, [equality], [message])+ ensures that the equality condition is met on all the selected elements through the selector starting from the _element_ (instance of +HTML::Node+) and its descendants.
558
+
559
+ For example, you could verify the contents on the title element in your response with:
560
+
561
+ <ruby>
562
+ assert_select 'title', "Welcome to Rails Testing Guide"
563
+ </ruby>
564
+
565
+ You can also use nested +assert_select+ blocks. In this case the inner +assert_select+ runs the assertion on the complete collection of elements selected by the outer +assert_select+ block:
566
+
567
+ <ruby>
568
+ assert_select 'ul.navigation' do
569
+ assert_select 'li.menu_item'
570
+ end
571
+ </ruby>
572
+
573
+ Alternatively the collection of elements selected by the outer +assert_select+ may be iterated through so that +assert_select+ may be called separately for each element. Suppose for example that the response contains two ordered lists, each with four list elements then the following tests will both pass.
574
+
575
+ <ruby>
576
+ assert_select "ol" do |elements|
577
+ elements.each do |element|
578
+ assert_select element, "li", 4
579
+ end
580
+ end
581
+
582
+ assert_select "ol" do
583
+ assert_select "li", 8
584
+ end
585
+ </ruby>
586
+
587
+ The +assert_select+ assertion is quite powerful. For more advanced usage, refer to its "documentation":http://api.rubyonrails.org/classes/ActionDispatch/Assertions/SelectorAssertions.html.
588
+
589
+ h5. Additional View-Based Assertions
590
+
591
+ There are more assertions that are primarily used in testing views:
592
+
593
+ |_.Assertion |_.Purpose|
594
+ |+assert_select_email+ |Allows you to make assertions on the body of an e-mail. |
595
+ |+assert_select_encoded+ |Allows you to make assertions on encoded HTML. It does this by un-encoding the contents of each element and then calling the block with all the un-encoded elements.|
596
+ |+css_select(selector)+ or +css_select(element, selector)+ |Returns an array of all the elements selected by the _selector_. In the second variant it first matches the base _element_ and tries to match the _selector_ expression on any of its children. If there are no matches both variants return an empty array.|
597
+
598
+ Here's an example of using +assert_select_email+:
599
+
600
+ <ruby>
601
+ assert_select_email do
602
+ assert_select 'small', 'Please click the "Unsubscribe" link if you want to opt-out.'
603
+ end
604
+ </ruby>
605
+
606
+ h3. Integration Testing
607
+
608
+ Integration tests are used to test the interaction among any number of controllers. They are generally used to test important work flows within your application.
609
+
610
+ Unlike Unit and Functional tests, integration tests have to be explicitly created under the 'test/integration' folder within your application. Rails provides a generator to create an integration test skeleton for you.
611
+
612
+ <shell>
613
+ $ rails generate integration_test user_flows
614
+ exists test/integration/
615
+ create test/integration/user_flows_test.rb
616
+ </shell>
617
+
618
+ Here's what a freshly-generated integration test looks like:
619
+
620
+ <ruby>
621
+ require 'test_helper'
622
+
623
+ class UserFlowsTest < ActionDispatch::IntegrationTest
624
+ fixtures :all
625
+
626
+ # Replace this with your real tests.
627
+ test "the truth" do
628
+ assert true
629
+ end
630
+ end
631
+ </ruby>
632
+
633
+ Integration tests inherit from +ActionDispatch::IntegrationTest+. This makes available some additional helpers to use in your integration tests. Also you need to explicitly include the fixtures to be made available to the test.
634
+
635
+ h4. Helpers Available for Integration Tests
636
+
637
+ In addition to the standard testing helpers, there are some additional helpers available to integration tests:
638
+
639
+ |_.Helper |_.Purpose|
640
+ |+https?+ |Returns +true+ if the session is mimicking a secure HTTPS request.|
641
+ |+https!+ |Allows you to mimic a secure HTTPS request.|
642
+ |+host!+ |Allows you to set the host name to use in the next request.|
643
+ |+redirect?+ |Returns +true+ if the last request was a redirect.|
644
+ |+follow_redirect!+ |Follows a single redirect response.|
645
+ |+request_via_redirect(http_method, path, [parameters], [headers])+ |Allows you to make an HTTP request and follow any subsequent redirects.|
646
+ |+post_via_redirect(path, [parameters], [headers])+ |Allows you to make an HTTP POST request and follow any subsequent redirects.|
647
+ |+get_via_redirect(path, [parameters], [headers])+ |Allows you to make an HTTP GET request and follow any subsequent redirects.|
648
+ |+put_via_redirect(path, [parameters], [headers])+ |Allows you to make an HTTP PUT request and follow any subsequent redirects.|
649
+ |+delete_via_redirect(path, [parameters], [headers])+ |Allows you to make an HTTP DELETE request and follow any subsequent redirects.|
650
+ |+open_session+ |Opens a new session instance.|
651
+
652
+ h4. Integration Testing Examples
653
+
654
+ A simple integration test that exercises multiple controllers:
655
+
656
+ <ruby>
657
+ require 'test_helper'
658
+
659
+ class UserFlowsTest < ActionDispatch::IntegrationTest
660
+ fixtures :users
661
+
662
+ test "login and browse site" do
663
+ # login via https
664
+ https!
665
+ get "/login"
666
+ assert_response :success
667
+
668
+ post_via_redirect "/login", :username => users(:avs).username, :password => users(:avs).password
669
+ assert_equal '/welcome', path
670
+ assert_equal 'Welcome avs!', flash[:notice]
671
+
672
+ https!(false)
673
+ get "/posts/all"
674
+ assert_response :success
675
+ assert assigns(:products)
676
+ end
677
+ end
678
+ </ruby>
679
+
680
+ As you can see the integration test involves multiple controllers and exercises the entire stack from database to dispatcher. In addition you can have multiple session instances open simultaneously in a test and extend those instances with assertion methods to create a very powerful testing DSL (domain-specific language) just for your application.
681
+
682
+ Here's an example of multiple sessions and custom DSL in an integration test
683
+
684
+ <ruby>
685
+ require 'test_helper'
686
+
687
+ class UserFlowsTest < ActionDispatch::IntegrationTest
688
+ fixtures :users
689
+
690
+ test "login and browse site" do
691
+
692
+ # User avs logs in
693
+ avs = login(:avs)
694
+ # User guest logs in
695
+ guest = login(:guest)
696
+
697
+ # Both are now available in different sessions
698
+ assert_equal 'Welcome avs!', avs.flash[:notice]
699
+ assert_equal 'Welcome guest!', guest.flash[:notice]
700
+
701
+ # User avs can browse site
702
+ avs.browses_site
703
+ # User guest can browse site as well
704
+ guest.browses_site
705
+
706
+ # Continue with other assertions
707
+ end
708
+
709
+ private
710
+
711
+ module CustomDsl
712
+ def browses_site
713
+ get "/products/all"
714
+ assert_response :success
715
+ assert assigns(:products)
716
+ end
717
+ end
718
+
719
+ def login(user)
720
+ open_session do |sess|
721
+ sess.extend(CustomDsl)
722
+ u = users(user)
723
+ sess.https!
724
+ sess.post "/login", :username => u.username, :password => u.password
725
+ assert_equal '/welcome', path
726
+ sess.https!(false)
727
+ end
728
+ end
729
+ end
730
+ </ruby>
731
+
732
+ h3. Rake Tasks for Running your Tests
733
+
734
+ You don't need to set up and run your tests by hand on a test-by-test basis. Rails comes with a number of rake tasks to help in testing. The table below lists all rake tasks that come along in the default Rakefile when you initiate a Rails project.
735
+
736
+ |_.Tasks |_.Description|
737
+ |+rake test+ |Runs all unit, functional and integration tests. You can also simply run +rake+ as the _test_ target is the default.|
738
+ |+rake test:benchmark+ |Benchmark the performance tests|
739
+ |+rake test:functionals+ |Runs all the functional tests from +test/functional+|
740
+ |+rake test:integration+ |Runs all the integration tests from +test/integration+|
741
+ |+rake test:plugins+ |Run all the plugin tests from +vendor/plugins/*/**/test+ (or specify with +PLUGIN=_name_+)|
742
+ |+rake test:profile+ |Profile the performance tests|
743
+ |+rake test:recent+ |Tests recent changes|
744
+ |+rake test:uncommitted+ |Runs all the tests which are uncommitted. Supports Subversion and Git|
745
+ |+rake test:units+ |Runs all the unit tests from +test/unit+|
746
+
747
+
748
+ h3. Brief Note About +Test::Unit+
749
+
750
+ Ruby ships with a boat load of libraries. One little gem of a library is +Test::Unit+, a framework for unit testing in Ruby. All the basic assertions discussed above are actually defined in +Test::Unit::Assertions+. The class +ActiveSupport::TestCase+ which we have been using in our unit and functional tests extends +Test::Unit::TestCase+, allowing
751
+ us to use all of the basic assertions in our tests.
752
+
753
+ NOTE: For more information on +Test::Unit+, refer to "test/unit Documentation":http://ruby-doc.org/stdlib/libdoc/test/unit/rdoc/
754
+
755
+ h3. Setup and Teardown
756
+
757
+ If you would like to run a block of code before the start of each test and another block of code after the end of each test you have two special callbacks for your rescue. Let's take note of this by looking at an example for our functional test in +Posts+ controller:
758
+
759
+ <ruby>
760
+ require 'test_helper'
761
+
762
+ class PostsControllerTest < ActionController::TestCase
763
+
764
+ # called before every single test
765
+ def setup
766
+ @post = posts(:one)
767
+ end
768
+
769
+ # called after every single test
770
+ def teardown
771
+ # as we are re-initializing @post before every test
772
+ # setting it to nil here is not essential but I hope
773
+ # you understand how you can use the teardown method
774
+ @post = nil
775
+ end
776
+
777
+ test "should show post" do
778
+ get :show, :id => @post.id
779
+ assert_response :success
780
+ end
781
+
782
+ test "should destroy post" do
783
+ assert_difference('Post.count', -1) do
784
+ delete :destroy, :id => @post.id
785
+ end
786
+
787
+ assert_redirected_to posts_path
788
+ end
789
+
790
+ end
791
+ </ruby>
792
+
793
+ Above, the +setup+ method is called before each test and so +@post+ is available for each of the tests. Rails implements +setup+ and +teardown+ as +ActiveSupport::Callbacks+. Which essentially means you need not only use +setup+ and +teardown+ as methods in your tests. You could specify them by using:
794
+
795
+ * a block
796
+ * a method (like in the earlier example)
797
+ * a method name as a symbol
798
+ * a lambda
799
+
800
+ Let's see the earlier example by specifying +setup+ callback by specifying a method name as a symbol:
801
+
802
+ <ruby>
803
+ require '../test_helper'
804
+
805
+ class PostsControllerTest < ActionController::TestCase
806
+
807
+ # called before every single test
808
+ setup :initialize_post
809
+
810
+ # called after every single test
811
+ def teardown
812
+ @post = nil
813
+ end
814
+
815
+ test "should show post" do
816
+ get :show, :id => @post.id
817
+ assert_response :success
818
+ end
819
+
820
+ test "should update post" do
821
+ put :update, :id => @post.id, :post => { }
822
+ assert_redirected_to post_path(assigns(:post))
823
+ end
824
+
825
+ test "should destroy post" do
826
+ assert_difference('Post.count', -1) do
827
+ delete :destroy, :id => @post.id
828
+ end
829
+
830
+ assert_redirected_to posts_path
831
+ end
832
+
833
+ private
834
+
835
+ def initialize_post
836
+ @post = posts(:one)
837
+ end
838
+
839
+ end
840
+ </ruby>
841
+
842
+ h3. Testing Routes
843
+
844
+ Like everything else in your Rails application, it is recommended that you test your routes. An example test for a route in the default +show+ action of +Posts+ controller above should look like:
845
+
846
+ <ruby>
847
+ test "should route to post" do
848
+ assert_routing '/posts/1', { :controller => "posts", :action => "show", :id => "1" }
849
+ end
850
+ </ruby>
851
+
852
+ h3. Testing Your Mailers
853
+
854
+ Testing mailer classes requires some specific tools to do a thorough job.
855
+
856
+ h4. Keeping the Postman in Check
857
+
858
+ Your mailer classes -- like every other part of your Rails application -- should be tested to ensure that it is working as expected.
859
+
860
+ The goals of testing your mailer classes are to ensure that:
861
+
862
+ * emails are being processed (created and sent)
863
+ * the email content is correct (subject, sender, body, etc)
864
+ * the right emails are being sent at the right times
865
+
866
+ h5. From All Sides
867
+
868
+ There are two aspects of testing your mailer, the unit tests and the functional tests. In the unit tests, you run the mailer in isolation with tightly controlled inputs and compare the output to a known value (a fixture.) In the functional tests you don't so much test the minute details produced by the mailer; instead, we test that our controllers and models are using the mailer in the right way. You test to prove that the right email was sent at the right time.
869
+
870
+ h4. Unit Testing
871
+
872
+ In order to test that your mailer is working as expected, you can use unit tests to compare the actual results of the mailer with pre-written examples of what should be produced.
873
+
874
+ h5. Revenge of the Fixtures
875
+
876
+ For the purposes of unit testing a mailer, fixtures are used to provide an example of how the output _should_ look. Because these are example emails, and not Active Record data like the other fixtures, they are kept in their own subdirectory apart from the other fixtures. The name of the directory within +test/fixtures+ directly corresponds to the name of the mailer. So, for a mailer named +UserMailer+, the fixtures should reside in +test/fixtures/user_mailer+ directory.
877
+
878
+ When you generated your mailer, the generator creates stub fixtures for each of the mailers actions. If you didn't use the generator you'll have to make those files yourself.
879
+
880
+ h5. The Basic Test Case
881
+
882
+ Here's a unit test to test a mailer named +UserMailer+ whose action +invite+ is used to send an invitation to a friend. It is an adapted version of the base test created by the generator for an +invite+ action.
883
+
884
+ <ruby>
885
+ require 'test_helper'
886
+
887
+ class UserMailerTest < ActionMailer::TestCase
888
+ tests UserMailer
889
+ test "invite" do
890
+ @expected.from = 'me@example.com'
891
+ @expected.to = 'friend@example.com'
892
+ @expected.subject = "You have been invited by #{@expected.from}"
893
+ @expected.body = read_fixture('invite')
894
+ @expected.date = Time.now
895
+
896
+ assert_equal @expected.encoded, UserMailer.create_invite('me@example.com', 'friend@example.com', @expected.date).encoded
897
+ end
898
+
899
+ end
900
+ </ruby>
901
+
902
+ In this test, +@expected+ is an instance of +TMail::Mail+ that you can use in your tests. It is defined in +ActionMailer::TestCase+. The test above uses +@expected+ to construct an email, which it then asserts with email created by the custom mailer. The +invite+ fixture is the body of the email and is used as the sample content to assert against. The helper +read_fixture+ is used to read in the content from this file.
903
+
904
+ Here's the content of the +invite+ fixture:
905
+
906
+ <pre>
907
+ Hi friend@example.com,
908
+
909
+ You have been invited.
910
+
911
+ Cheers!
912
+ </pre>
913
+
914
+ This is the right time to understand a little more about writing tests for your mailers. The line +ActionMailer::Base.delivery_method = :test+ in +config/environments/test.rb+ sets the delivery method to test mode so that email will not actually be delivered (useful to avoid spamming your users while testing) but instead it will be appended to an array (+ActionMailer::Base.deliveries+).
915
+
916
+ However often in unit tests, mails will not actually be sent, simply constructed, as in the example above, where the precise content of the email is checked against what it should be.
917
+
918
+ h4. Functional Testing
919
+
920
+ Functional testing for mailers involves more than just checking that the email body, recipients and so forth are correct. In functional mail tests you call the mail deliver methods and check that the appropriate emails have been appended to the delivery list. It is fairly safe to assume that the deliver methods themselves do their job. You are probably more interested in whether your own business logic is sending emails when you expect them to go out. For example, you can check that the invite friend operation is sending an email appropriately:
921
+
922
+ <ruby>
923
+ require 'test_helper'
924
+
925
+ class UserControllerTest < ActionController::TestCase
926
+ test "invite friend" do
927
+ assert_difference 'ActionMailer::Base.deliveries.size', +1 do
928
+ post :invite_friend, :email => 'friend@example.com'
929
+ end
930
+ invite_email = ActionMailer::Base.deliveries.last
931
+
932
+ assert_equal "You have been invited by me@example.com", invite_email.subject
933
+ assert_equal 'friend@example.com', invite_email.to[0]
934
+ assert_match(/Hi friend@example.com/, invite_email.body)
935
+ end
936
+ end
937
+ </ruby>
938
+
939
+ h3. Other Testing Approaches
940
+
941
+ The built-in +test/unit+ based testing is not the only way to test Rails applications. Rails developers have come up with a wide variety of other approaches and aids for testing, including:
942
+
943
+ * "NullDB":http://avdi.org/projects/nulldb/, a way to speed up testing by avoiding database use.
944
+ * "Factory Girl":https://github.com/thoughtbot/factory_girl/tree/master, a replacement for fixtures.
945
+ * "Machinist":https://github.com/notahat/machinist/tree/master, another replacement for fixtures.
946
+ * "Shoulda":http://www.thoughtbot.com/projects/shoulda, an extension to +test/unit+ with additional helpers, macros, and assertions.
947
+ * "RSpec":http://relishapp.com/rspec, a behavior-driven development framework