railties 3.2.22.5 → 4.0.0.beta1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (450) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +186 -78
  3. data/README.rdoc +4 -2
  4. data/lib/rails.rb +11 -44
  5. data/lib/rails/all.rb +0 -1
  6. data/lib/rails/app_rails_loader.rb +37 -0
  7. data/lib/rails/application.rb +176 -124
  8. data/lib/rails/application/bootstrap.rb +29 -23
  9. data/lib/rails/application/configuration.rb +54 -48
  10. data/lib/rails/application/finisher.rb +8 -12
  11. data/lib/rails/application/routes_reloader.rb +2 -2
  12. data/lib/rails/backtrace_cleaner.rb +1 -16
  13. data/lib/rails/cli.rb +2 -2
  14. data/lib/rails/code_statistics.rb +30 -45
  15. data/lib/rails/code_statistics_calculator.rb +79 -0
  16. data/lib/rails/commands.rb +44 -37
  17. data/lib/rails/commands/application.rb +14 -9
  18. data/lib/rails/commands/console.rb +77 -33
  19. data/lib/rails/commands/dbconsole.rb +115 -64
  20. data/lib/rails/commands/destroy.rb +2 -3
  21. data/lib/rails/commands/generate.rb +2 -3
  22. data/lib/rails/commands/plugin_new.rb +0 -2
  23. data/lib/rails/commands/runner.rb +4 -5
  24. data/lib/rails/commands/server.rb +28 -9
  25. data/lib/rails/commands/update.rb +1 -1
  26. data/lib/rails/configuration.rb +35 -8
  27. data/lib/rails/console/app.rb +0 -4
  28. data/lib/rails/deprecation.rb +19 -0
  29. data/lib/rails/engine.rb +111 -92
  30. data/lib/rails/engine/commands.rb +1 -3
  31. data/lib/rails/engine/configuration.rb +26 -19
  32. data/lib/rails/engine/railties.rb +14 -18
  33. data/lib/rails/generators.rb +30 -50
  34. data/lib/rails/generators/actions.rb +38 -103
  35. data/lib/rails/generators/active_model.rb +14 -6
  36. data/lib/rails/generators/app_base.rb +70 -83
  37. data/lib/rails/generators/base.rb +23 -35
  38. data/lib/rails/generators/css/assets/assets_generator.rb +3 -3
  39. data/lib/rails/generators/css/scaffold/scaffold_generator.rb +3 -3
  40. data/lib/rails/generators/erb.rb +2 -2
  41. data/lib/rails/generators/erb/controller/controller_generator.rb +4 -4
  42. data/lib/rails/generators/erb/mailer/mailer_generator.rb +3 -3
  43. data/lib/rails/generators/erb/scaffold/scaffold_generator.rb +4 -4
  44. data/lib/rails/generators/erb/scaffold/templates/index.html.erb +18 -14
  45. data/lib/rails/generators/erb/scaffold/templates/show.html.erb +1 -1
  46. data/lib/rails/generators/generated_attribute.rb +51 -16
  47. data/lib/rails/generators/js/assets/assets_generator.rb +3 -3
  48. data/lib/rails/generators/migration.rb +1 -5
  49. data/lib/rails/generators/named_base.rb +20 -25
  50. data/lib/rails/generators/rails/app/app_generator.rb +40 -48
  51. data/lib/rails/generators/rails/app/templates/Gemfile +8 -7
  52. data/lib/rails/generators/rails/app/templates/README.rdoc +28 -0
  53. data/lib/rails/generators/rails/app/templates/Rakefile +0 -1
  54. data/lib/rails/generators/rails/app/templates/app/assets/javascripts/application.js.tt +2 -1
  55. data/lib/rails/generators/rails/app/templates/app/controllers/application_controller.rb.tt +5 -0
  56. data/lib/rails/generators/rails/app/templates/app/views/layouts/application.html.erb.tt +2 -2
  57. data/lib/rails/generators/rails/app/templates/bin/bundle +2 -0
  58. data/lib/rails/generators/rails/app/templates/bin/rails +3 -0
  59. data/lib/rails/generators/rails/app/templates/bin/rake +3 -0
  60. data/lib/rails/generators/rails/app/templates/config/application.rb +5 -43
  61. data/lib/rails/generators/rails/app/templates/config/boot.rb +0 -2
  62. data/lib/rails/generators/rails/app/templates/config/databases/jdbcpostgresql.yml +2 -2
  63. data/lib/rails/generators/rails/app/templates/config/databases/mysql.yml +1 -4
  64. data/lib/rails/generators/rails/app/templates/config/databases/postgresql.yml +8 -3
  65. data/lib/rails/generators/rails/app/templates/config/databases/sqlserver.yml +57 -0
  66. data/lib/rails/generators/rails/app/templates/config/environment.rb +2 -2
  67. data/lib/rails/generators/rails/app/templates/config/environments/development.rb.tt +9 -19
  68. data/lib/rails/generators/rails/app/templates/config/environments/production.rb.tt +40 -29
  69. data/lib/rails/generators/rails/app/templates/config/environments/test.rb.tt +12 -15
  70. data/lib/rails/generators/rails/app/templates/config/initializers/filter_parameter_logging.rb +4 -0
  71. data/lib/rails/generators/rails/app/templates/config/initializers/inflections.rb +6 -5
  72. data/lib/rails/generators/rails/app/templates/config/initializers/secret_token.rb.tt +6 -1
  73. data/lib/rails/generators/rails/app/templates/config/initializers/session_store.rb.tt +1 -6
  74. data/lib/rails/generators/rails/app/templates/config/initializers/wrap_parameters.rb.tt +6 -6
  75. data/lib/rails/generators/rails/app/templates/config/locales/en.yml +20 -2
  76. data/lib/rails/generators/rails/app/templates/config/routes.rb +15 -24
  77. data/lib/rails/generators/rails/app/templates/db/seeds.rb.tt +2 -2
  78. data/lib/rails/generators/rails/app/templates/gitignore +3 -2
  79. data/lib/rails/generators/rails/app/templates/public/404.html +2 -1
  80. data/lib/rails/generators/rails/app/templates/public/422.html +1 -1
  81. data/lib/rails/generators/rails/app/templates/public/500.html +2 -1
  82. data/lib/rails/generators/rails/app/templates/public/robots.txt +1 -1
  83. data/lib/rails/generators/rails/app/templates/test/test_helper.rb +3 -1
  84. data/lib/rails/generators/rails/assets/assets_generator.rb +5 -5
  85. data/lib/rails/generators/rails/controller/USAGE +7 -7
  86. data/lib/rails/generators/rails/controller/controller_generator.rb +3 -3
  87. data/lib/rails/generators/rails/controller/templates/controller.rb +1 -1
  88. data/lib/rails/generators/rails/generator/generator_generator.rb +4 -4
  89. data/lib/rails/generators/rails/helper/USAGE +1 -1
  90. data/lib/rails/generators/rails/helper/helper_generator.rb +2 -2
  91. data/lib/rails/generators/rails/integration_test/integration_test_generator.rb +2 -2
  92. data/lib/rails/generators/rails/migration/USAGE +12 -6
  93. data/lib/rails/generators/rails/migration/migration_generator.rb +3 -3
  94. data/lib/rails/generators/rails/model/USAGE +53 -4
  95. data/lib/rails/generators/rails/model/model_generator.rb +3 -3
  96. data/lib/rails/generators/rails/plugin_new/plugin_new_generator.rb +81 -39
  97. data/lib/rails/generators/rails/plugin_new/templates/%name%.gemspec +2 -2
  98. data/lib/rails/generators/rails/plugin_new/templates/Gemfile +16 -1
  99. data/lib/rails/generators/rails/plugin_new/templates/Rakefile +3 -9
  100. data/lib/rails/generators/rails/plugin_new/templates/app/controllers/%name%/application_controller.rb.tt +0 -1
  101. data/lib/rails/generators/rails/plugin_new/templates/app/views/layouts/%name%/application.html.erb.tt +1 -1
  102. data/lib/rails/generators/rails/plugin_new/templates/{script → bin}/rails.tt +0 -0
  103. data/lib/rails/generators/rails/plugin_new/templates/gitignore +3 -0
  104. data/lib/rails/generators/rails/plugin_new/templates/lib/%name%.rb +1 -1
  105. data/lib/rails/generators/rails/plugin_new/templates/rails/application.rb +0 -1
  106. data/lib/rails/generators/rails/plugin_new/templates/rails/boot.rb +0 -1
  107. data/lib/rails/generators/rails/resource/resource_generator.rb +5 -5
  108. data/lib/rails/generators/rails/resource_route/resource_route_generator.rb +2 -2
  109. data/lib/rails/generators/rails/scaffold/scaffold_generator.rb +11 -7
  110. data/lib/rails/generators/rails/scaffold_controller/USAGE +4 -4
  111. data/lib/rails/generators/rails/scaffold_controller/scaffold_controller_generator.rb +9 -7
  112. data/lib/rails/generators/rails/scaffold_controller/templates/controller.rb +27 -48
  113. data/lib/rails/generators/rails/task/task_generator.rb +2 -2
  114. data/lib/rails/generators/rails/task/templates/task.rb +1 -1
  115. data/lib/rails/generators/resource_helpers.rb +3 -5
  116. data/lib/rails/generators/test_case.rb +22 -28
  117. data/lib/rails/generators/test_unit.rb +3 -3
  118. data/lib/rails/generators/test_unit/controller/controller_generator.rb +6 -6
  119. data/lib/rails/generators/test_unit/helper/helper_generator.rb +5 -5
  120. data/lib/rails/generators/test_unit/integration/integration_generator.rb +4 -4
  121. data/lib/rails/generators/test_unit/mailer/mailer_generator.rb +6 -6
  122. data/lib/rails/generators/test_unit/model/model_generator.rb +7 -7
  123. data/lib/rails/generators/test_unit/model/templates/fixtures.yml +8 -2
  124. data/lib/rails/generators/test_unit/plugin/plugin_generator.rb +4 -4
  125. data/lib/rails/generators/test_unit/plugin/templates/test_helper.rb +1 -2
  126. data/lib/rails/generators/test_unit/scaffold/scaffold_generator.rb +10 -19
  127. data/lib/rails/generators/test_unit/scaffold/templates/functional_test.rb +5 -5
  128. data/lib/rails/info.rb +3 -3
  129. data/lib/rails/info_controller.rb +26 -8
  130. data/lib/rails/initializable.rb +2 -2
  131. data/lib/rails/paths.rb +59 -54
  132. data/lib/rails/rack/debugger.rb +2 -2
  133. data/lib/rails/rack/logger.rb +33 -8
  134. data/lib/rails/railtie.rb +24 -24
  135. data/lib/rails/railtie/configurable.rb +1 -1
  136. data/lib/rails/railtie/configuration.rb +15 -5
  137. data/lib/rails/ruby_version_check.rb +3 -13
  138. data/lib/rails/rubyprof_ext.rb +1 -1
  139. data/lib/rails/source_annotation_extractor.rb +40 -17
  140. data/lib/rails/tasks/annotations.rake +1 -1
  141. data/lib/rails/tasks/documentation.rake +10 -57
  142. data/lib/rails/tasks/engine.rake +1 -1
  143. data/lib/rails/tasks/framework.rake +9 -9
  144. data/lib/rails/tasks/log.rake +18 -4
  145. data/lib/rails/tasks/middleware.rake +1 -1
  146. data/lib/rails/tasks/misc.rake +2 -9
  147. data/lib/rails/tasks/routes.rake +4 -6
  148. data/lib/rails/tasks/statistics.rake +8 -2
  149. data/lib/rails/tasks/tmp.rake +12 -4
  150. data/lib/rails/templates/layouts/application.html.erb +36 -0
  151. data/lib/rails/templates/rails/info/properties.html.erb +1 -0
  152. data/lib/rails/templates/rails/info/routes.html.erb +9 -0
  153. data/lib/rails/{generators/rails/app/templates/public/index.html → templates/rails/welcome/index.html.erb} +18 -14
  154. data/lib/rails/test_help.rb +6 -29
  155. data/lib/rails/test_unit/railtie.rb +2 -3
  156. data/lib/rails/test_unit/testing.rake +45 -33
  157. data/lib/rails/version.rb +4 -4
  158. data/lib/rails/welcome_controller.rb +7 -0
  159. metadata +86 -378
  160. data/guides/assets/images/belongs_to.png +0 -0
  161. data/guides/assets/images/book_icon.gif +0 -0
  162. data/guides/assets/images/bullet.gif +0 -0
  163. data/guides/assets/images/challenge.png +0 -0
  164. data/guides/assets/images/chapters_icon.gif +0 -0
  165. data/guides/assets/images/check_bullet.gif +0 -0
  166. data/guides/assets/images/credits_pic_blank.gif +0 -0
  167. data/guides/assets/images/csrf.png +0 -0
  168. data/guides/assets/images/customized_error_messages.png +0 -0
  169. data/guides/assets/images/edge_badge.png +0 -0
  170. data/guides/assets/images/error_messages.png +0 -0
  171. data/guides/assets/images/feature_tile.gif +0 -0
  172. data/guides/assets/images/footer_tile.gif +0 -0
  173. data/guides/assets/images/fxn.png +0 -0
  174. data/guides/assets/images/grey_bullet.gif +0 -0
  175. data/guides/assets/images/habtm.png +0 -0
  176. data/guides/assets/images/has_many.png +0 -0
  177. data/guides/assets/images/has_many_through.png +0 -0
  178. data/guides/assets/images/has_one.png +0 -0
  179. data/guides/assets/images/has_one_through.png +0 -0
  180. data/guides/assets/images/header_backdrop.png +0 -0
  181. data/guides/assets/images/header_tile.gif +0 -0
  182. data/guides/assets/images/i18n/demo_html_safe.png +0 -0
  183. data/guides/assets/images/i18n/demo_localized_pirate.png +0 -0
  184. data/guides/assets/images/i18n/demo_translated_en.png +0 -0
  185. data/guides/assets/images/i18n/demo_translated_pirate.png +0 -0
  186. data/guides/assets/images/i18n/demo_translation_missing.png +0 -0
  187. data/guides/assets/images/i18n/demo_untranslated.png +0 -0
  188. data/guides/assets/images/icons/README +0 -5
  189. data/guides/assets/images/icons/callouts/1.png +0 -0
  190. data/guides/assets/images/icons/callouts/10.png +0 -0
  191. data/guides/assets/images/icons/callouts/11.png +0 -0
  192. data/guides/assets/images/icons/callouts/12.png +0 -0
  193. data/guides/assets/images/icons/callouts/13.png +0 -0
  194. data/guides/assets/images/icons/callouts/14.png +0 -0
  195. data/guides/assets/images/icons/callouts/15.png +0 -0
  196. data/guides/assets/images/icons/callouts/2.png +0 -0
  197. data/guides/assets/images/icons/callouts/3.png +0 -0
  198. data/guides/assets/images/icons/callouts/4.png +0 -0
  199. data/guides/assets/images/icons/callouts/5.png +0 -0
  200. data/guides/assets/images/icons/callouts/6.png +0 -0
  201. data/guides/assets/images/icons/callouts/7.png +0 -0
  202. data/guides/assets/images/icons/callouts/8.png +0 -0
  203. data/guides/assets/images/icons/callouts/9.png +0 -0
  204. data/guides/assets/images/icons/caution.png +0 -0
  205. data/guides/assets/images/icons/example.png +0 -0
  206. data/guides/assets/images/icons/home.png +0 -0
  207. data/guides/assets/images/icons/important.png +0 -0
  208. data/guides/assets/images/icons/next.png +0 -0
  209. data/guides/assets/images/icons/note.png +0 -0
  210. data/guides/assets/images/icons/prev.png +0 -0
  211. data/guides/assets/images/icons/tip.png +0 -0
  212. data/guides/assets/images/icons/up.png +0 -0
  213. data/guides/assets/images/icons/warning.png +0 -0
  214. data/guides/assets/images/jaimeiniesta.jpg +0 -0
  215. data/guides/assets/images/nav_arrow.gif +0 -0
  216. data/guides/assets/images/polymorphic.png +0 -0
  217. data/guides/assets/images/posts_index.png +0 -0
  218. data/guides/assets/images/radar.png +0 -0
  219. data/guides/assets/images/rails_guides_kindle_cover.jpg +0 -0
  220. data/guides/assets/images/rails_guides_logo.gif +0 -0
  221. data/guides/assets/images/rails_logo_remix.gif +0 -0
  222. data/guides/assets/images/rails_welcome.png +0 -0
  223. data/guides/assets/images/session_fixation.png +0 -0
  224. data/guides/assets/images/tab_grey.gif +0 -0
  225. data/guides/assets/images/tab_info.gif +0 -0
  226. data/guides/assets/images/tab_note.gif +0 -0
  227. data/guides/assets/images/tab_red.gif +0 -0
  228. data/guides/assets/images/tab_yellow.gif +0 -0
  229. data/guides/assets/images/tab_yellow.png +0 -0
  230. data/guides/assets/images/validation_error_messages.png +0 -0
  231. data/guides/assets/images/vijaydev.jpg +0 -0
  232. data/guides/assets/javascripts/guides.js +0 -7
  233. data/guides/assets/javascripts/syntaxhighlighter/shBrushAS3.js +0 -59
  234. data/guides/assets/javascripts/syntaxhighlighter/shBrushAppleScript.js +0 -75
  235. data/guides/assets/javascripts/syntaxhighlighter/shBrushBash.js +0 -59
  236. data/guides/assets/javascripts/syntaxhighlighter/shBrushCSharp.js +0 -65
  237. data/guides/assets/javascripts/syntaxhighlighter/shBrushColdFusion.js +0 -100
  238. data/guides/assets/javascripts/syntaxhighlighter/shBrushCpp.js +0 -97
  239. data/guides/assets/javascripts/syntaxhighlighter/shBrushCss.js +0 -91
  240. data/guides/assets/javascripts/syntaxhighlighter/shBrushDelphi.js +0 -55
  241. data/guides/assets/javascripts/syntaxhighlighter/shBrushDiff.js +0 -41
  242. data/guides/assets/javascripts/syntaxhighlighter/shBrushErlang.js +0 -52
  243. data/guides/assets/javascripts/syntaxhighlighter/shBrushGroovy.js +0 -67
  244. data/guides/assets/javascripts/syntaxhighlighter/shBrushJScript.js +0 -52
  245. data/guides/assets/javascripts/syntaxhighlighter/shBrushJava.js +0 -57
  246. data/guides/assets/javascripts/syntaxhighlighter/shBrushJavaFX.js +0 -58
  247. data/guides/assets/javascripts/syntaxhighlighter/shBrushPerl.js +0 -72
  248. data/guides/assets/javascripts/syntaxhighlighter/shBrushPhp.js +0 -88
  249. data/guides/assets/javascripts/syntaxhighlighter/shBrushPlain.js +0 -33
  250. data/guides/assets/javascripts/syntaxhighlighter/shBrushPowerShell.js +0 -74
  251. data/guides/assets/javascripts/syntaxhighlighter/shBrushPython.js +0 -64
  252. data/guides/assets/javascripts/syntaxhighlighter/shBrushRuby.js +0 -55
  253. data/guides/assets/javascripts/syntaxhighlighter/shBrushSass.js +0 -94
  254. data/guides/assets/javascripts/syntaxhighlighter/shBrushScala.js +0 -51
  255. data/guides/assets/javascripts/syntaxhighlighter/shBrushSql.js +0 -66
  256. data/guides/assets/javascripts/syntaxhighlighter/shBrushVb.js +0 -56
  257. data/guides/assets/javascripts/syntaxhighlighter/shBrushXml.js +0 -69
  258. data/guides/assets/javascripts/syntaxhighlighter/shCore.js +0 -17
  259. data/guides/assets/stylesheets/fixes.css +0 -16
  260. data/guides/assets/stylesheets/kindle.css +0 -11
  261. data/guides/assets/stylesheets/main.css +0 -453
  262. data/guides/assets/stylesheets/print.css +0 -52
  263. data/guides/assets/stylesheets/reset.css +0 -43
  264. data/guides/assets/stylesheets/style.css +0 -13
  265. data/guides/assets/stylesheets/syntaxhighlighter/shCore.css +0 -226
  266. data/guides/assets/stylesheets/syntaxhighlighter/shCoreDefault.css +0 -328
  267. data/guides/assets/stylesheets/syntaxhighlighter/shCoreDjango.css +0 -331
  268. data/guides/assets/stylesheets/syntaxhighlighter/shCoreEclipse.css +0 -339
  269. data/guides/assets/stylesheets/syntaxhighlighter/shCoreEmacs.css +0 -324
  270. data/guides/assets/stylesheets/syntaxhighlighter/shCoreFadeToGrey.css +0 -328
  271. data/guides/assets/stylesheets/syntaxhighlighter/shCoreMDUltra.css +0 -324
  272. data/guides/assets/stylesheets/syntaxhighlighter/shCoreMidnight.css +0 -324
  273. data/guides/assets/stylesheets/syntaxhighlighter/shCoreRDark.css +0 -324
  274. data/guides/assets/stylesheets/syntaxhighlighter/shThemeDefault.css +0 -117
  275. data/guides/assets/stylesheets/syntaxhighlighter/shThemeDjango.css +0 -120
  276. data/guides/assets/stylesheets/syntaxhighlighter/shThemeEclipse.css +0 -128
  277. data/guides/assets/stylesheets/syntaxhighlighter/shThemeEmacs.css +0 -113
  278. data/guides/assets/stylesheets/syntaxhighlighter/shThemeFadeToGrey.css +0 -117
  279. data/guides/assets/stylesheets/syntaxhighlighter/shThemeMDUltra.css +0 -113
  280. data/guides/assets/stylesheets/syntaxhighlighter/shThemeMidnight.css +0 -113
  281. data/guides/assets/stylesheets/syntaxhighlighter/shThemeRDark.css +0 -113
  282. data/guides/assets/stylesheets/syntaxhighlighter/shThemeRailsGuides.css +0 -116
  283. data/guides/code/getting_started/Gemfile +0 -38
  284. data/guides/code/getting_started/README.rdoc +0 -261
  285. data/guides/code/getting_started/Rakefile +0 -7
  286. data/guides/code/getting_started/app/assets/images/rails.png +0 -0
  287. data/guides/code/getting_started/app/assets/javascripts/application.js +0 -15
  288. data/guides/code/getting_started/app/assets/javascripts/comments.js.coffee +0 -3
  289. data/guides/code/getting_started/app/assets/javascripts/home.js.coffee +0 -3
  290. data/guides/code/getting_started/app/assets/javascripts/posts.js.coffee +0 -3
  291. data/guides/code/getting_started/app/assets/stylesheets/application.css +0 -13
  292. data/guides/code/getting_started/app/assets/stylesheets/comments.css.scss +0 -3
  293. data/guides/code/getting_started/app/assets/stylesheets/home.css.scss +0 -3
  294. data/guides/code/getting_started/app/assets/stylesheets/posts.css.scss +0 -3
  295. data/guides/code/getting_started/app/assets/stylesheets/scaffolds.css.scss +0 -56
  296. data/guides/code/getting_started/app/controllers/application_controller.rb +0 -3
  297. data/guides/code/getting_started/app/controllers/comments_controller.rb +0 -16
  298. data/guides/code/getting_started/app/controllers/home_controller.rb +0 -5
  299. data/guides/code/getting_started/app/controllers/posts_controller.rb +0 -84
  300. data/guides/code/getting_started/app/helpers/application_helper.rb +0 -2
  301. data/guides/code/getting_started/app/helpers/comments_helper.rb +0 -2
  302. data/guides/code/getting_started/app/helpers/home_helper.rb +0 -2
  303. data/guides/code/getting_started/app/helpers/posts_helper.rb +0 -5
  304. data/guides/code/getting_started/app/models/comment.rb +0 -3
  305. data/guides/code/getting_started/app/models/post.rb +0 -11
  306. data/guides/code/getting_started/app/models/tag.rb +0 -3
  307. data/guides/code/getting_started/app/views/comments/_comment.html.erb +0 -15
  308. data/guides/code/getting_started/app/views/comments/_form.html.erb +0 -13
  309. data/guides/code/getting_started/app/views/home/index.html.erb +0 -2
  310. data/guides/code/getting_started/app/views/layouts/application.html.erb +0 -14
  311. data/guides/code/getting_started/app/views/posts/_form.html.erb +0 -32
  312. data/guides/code/getting_started/app/views/posts/edit.html.erb +0 -6
  313. data/guides/code/getting_started/app/views/posts/index.html.erb +0 -27
  314. data/guides/code/getting_started/app/views/posts/new.html.erb +0 -5
  315. data/guides/code/getting_started/app/views/posts/show.html.erb +0 -31
  316. data/guides/code/getting_started/app/views/tags/_form.html.erb +0 -12
  317. data/guides/code/getting_started/config.ru +0 -4
  318. data/guides/code/getting_started/config/application.rb +0 -59
  319. data/guides/code/getting_started/config/boot.rb +0 -6
  320. data/guides/code/getting_started/config/database.yml +0 -25
  321. data/guides/code/getting_started/config/environment.rb +0 -5
  322. data/guides/code/getting_started/config/environments/development.rb +0 -37
  323. data/guides/code/getting_started/config/environments/production.rb +0 -67
  324. data/guides/code/getting_started/config/environments/test.rb +0 -37
  325. data/guides/code/getting_started/config/initializers/backtrace_silencers.rb +0 -7
  326. data/guides/code/getting_started/config/initializers/inflections.rb +0 -15
  327. data/guides/code/getting_started/config/initializers/mime_types.rb +0 -5
  328. data/guides/code/getting_started/config/initializers/secret_token.rb +0 -7
  329. data/guides/code/getting_started/config/initializers/session_store.rb +0 -8
  330. data/guides/code/getting_started/config/initializers/wrap_parameters.rb +0 -14
  331. data/guides/code/getting_started/config/locales/en.yml +0 -5
  332. data/guides/code/getting_started/config/routes.rb +0 -64
  333. data/guides/code/getting_started/db/migrate/20110901012504_create_posts.rb +0 -11
  334. data/guides/code/getting_started/db/migrate/20110901012815_create_comments.rb +0 -12
  335. data/guides/code/getting_started/db/migrate/20110901013701_create_tags.rb +0 -11
  336. data/guides/code/getting_started/db/schema.rb +0 -43
  337. data/guides/code/getting_started/db/seeds.rb +0 -7
  338. data/guides/code/getting_started/doc/README_FOR_APP +0 -2
  339. data/guides/code/getting_started/public/404.html +0 -26
  340. data/guides/code/getting_started/public/422.html +0 -26
  341. data/guides/code/getting_started/public/500.html +0 -25
  342. data/guides/code/getting_started/public/favicon.ico +0 -0
  343. data/guides/code/getting_started/public/robots.txt +0 -5
  344. data/guides/code/getting_started/script/rails +0 -6
  345. data/guides/code/getting_started/test/fixtures/comments.yml +0 -11
  346. data/guides/code/getting_started/test/fixtures/posts.yml +0 -11
  347. data/guides/code/getting_started/test/fixtures/tags.yml +0 -9
  348. data/guides/code/getting_started/test/functional/comments_controller_test.rb +0 -7
  349. data/guides/code/getting_started/test/functional/home_controller_test.rb +0 -9
  350. data/guides/code/getting_started/test/functional/posts_controller_test.rb +0 -49
  351. data/guides/code/getting_started/test/performance/browsing_test.rb +0 -12
  352. data/guides/code/getting_started/test/test_helper.rb +0 -13
  353. data/guides/code/getting_started/test/unit/comment_test.rb +0 -7
  354. data/guides/code/getting_started/test/unit/helpers/comments_helper_test.rb +0 -4
  355. data/guides/code/getting_started/test/unit/helpers/home_helper_test.rb +0 -4
  356. data/guides/code/getting_started/test/unit/helpers/posts_helper_test.rb +0 -4
  357. data/guides/code/getting_started/test/unit/post_test.rb +0 -7
  358. data/guides/code/getting_started/test/unit/tag_test.rb +0 -7
  359. data/guides/rails_guides.rb +0 -50
  360. data/guides/rails_guides/generator.rb +0 -310
  361. data/guides/rails_guides/helpers.rb +0 -45
  362. data/guides/rails_guides/indexer.rb +0 -69
  363. data/guides/rails_guides/levenshtein.rb +0 -31
  364. data/guides/rails_guides/textile_extensions.rb +0 -63
  365. data/guides/source/2_2_release_notes.textile +0 -422
  366. data/guides/source/2_3_release_notes.textile +0 -610
  367. data/guides/source/3_0_release_notes.textile +0 -595
  368. data/guides/source/3_1_release_notes.textile +0 -553
  369. data/guides/source/3_2_release_notes.textile +0 -540
  370. data/guides/source/_license.html.erb +0 -2
  371. data/guides/source/_welcome.html.erb +0 -19
  372. data/guides/source/action_controller_overview.textile +0 -820
  373. data/guides/source/action_mailer_basics.textile +0 -516
  374. data/guides/source/action_view_overview.textile +0 -1497
  375. data/guides/source/active_model_basics.textile +0 -205
  376. data/guides/source/active_record_basics.textile +0 -218
  377. data/guides/source/active_record_querying.textile +0 -1433
  378. data/guides/source/active_record_validations_callbacks.textile +0 -1287
  379. data/guides/source/active_resource_basics.textile +0 -120
  380. data/guides/source/active_support_core_extensions.textile +0 -3715
  381. data/guides/source/api_documentation_guidelines.textile +0 -185
  382. data/guides/source/asset_pipeline.textile +0 -707
  383. data/guides/source/association_basics.textile +0 -1959
  384. data/guides/source/caching_with_rails.textile +0 -437
  385. data/guides/source/command_line.textile +0 -574
  386. data/guides/source/configuring.textile +0 -643
  387. data/guides/source/contributing_to_ruby_on_rails.textile +0 -448
  388. data/guides/source/credits.html.erb +0 -72
  389. data/guides/source/debugging_rails_applications.textile +0 -714
  390. data/guides/source/documents.yaml +0 -157
  391. data/guides/source/engines.textile +0 -618
  392. data/guides/source/form_helpers.textile +0 -798
  393. data/guides/source/generators.textile +0 -621
  394. data/guides/source/getting_started.textile +0 -1931
  395. data/guides/source/i18n.textile +0 -931
  396. data/guides/source/index.html.erb +0 -30
  397. data/guides/source/initialization.textile +0 -1116
  398. data/guides/source/kindle/KINDLE.md +0 -26
  399. data/guides/source/kindle/copyright.html.erb +0 -1
  400. data/guides/source/kindle/layout.html.erb +0 -27
  401. data/guides/source/kindle/rails_guides.opf.erb +0 -52
  402. data/guides/source/kindle/toc.html.erb +0 -24
  403. data/guides/source/kindle/toc.ncx.erb +0 -64
  404. data/guides/source/kindle/welcome.html.erb +0 -5
  405. data/guides/source/layout.html.erb +0 -124
  406. data/guides/source/layouts_and_rendering.textile +0 -1237
  407. data/guides/source/migrations.textile +0 -900
  408. data/guides/source/nested_model_forms.textile +0 -222
  409. data/guides/source/performance_testing.textile +0 -597
  410. data/guides/source/plugins.textile +0 -464
  411. data/guides/source/rails_application_templates.textile +0 -240
  412. data/guides/source/rails_on_rack.textile +0 -236
  413. data/guides/source/routing.textile +0 -885
  414. data/guides/source/ruby_on_rails_guides_guidelines.textile +0 -79
  415. data/guides/source/security.textile +0 -1004
  416. data/guides/source/testing.textile +0 -947
  417. data/guides/w3c_validator.rb +0 -91
  418. data/lib/rails/application/railties.rb +0 -13
  419. data/lib/rails/application/route_inspector.rb +0 -84
  420. data/lib/rails/commands/benchmarker.rb +0 -34
  421. data/lib/rails/commands/plugin.rb +0 -544
  422. data/lib/rails/commands/profiler.rb +0 -33
  423. data/lib/rails/generators/rails/app/templates/README +0 -261
  424. data/lib/rails/generators/rails/app/templates/app/controllers/application_controller.rb +0 -3
  425. data/lib/rails/generators/rails/app/templates/app/mailers/.empty_directory +0 -0
  426. data/lib/rails/generators/rails/app/templates/app/models/.empty_directory +0 -0
  427. data/lib/rails/generators/rails/app/templates/doc/README_FOR_APP +0 -2
  428. data/lib/rails/generators/rails/app/templates/public/stylesheets/.empty_directory +0 -0
  429. data/lib/rails/generators/rails/app/templates/script/rails +0 -5
  430. data/lib/rails/generators/rails/app/templates/test/fixtures/.empty_directory +0 -0
  431. data/lib/rails/generators/rails/app/templates/test/functional/.empty_directory +0 -0
  432. data/lib/rails/generators/rails/app/templates/test/integration/.empty_directory +0 -0
  433. data/lib/rails/generators/rails/app/templates/test/performance/browsing_test.rb +0 -12
  434. data/lib/rails/generators/rails/app/templates/test/unit/.empty_directory +0 -0
  435. data/lib/rails/generators/rails/generator/templates/templates/.empty_directory +0 -0
  436. data/lib/rails/generators/rails/observer/USAGE +0 -12
  437. data/lib/rails/generators/rails/observer/observer_generator.rb +0 -7
  438. data/lib/rails/generators/rails/performance_test/USAGE +0 -10
  439. data/lib/rails/generators/rails/performance_test/performance_test_generator.rb +0 -7
  440. data/lib/rails/generators/rails/plugin_new/templates/app/mailers/.empty_directory +0 -0
  441. data/lib/rails/generators/rails/plugin_new/templates/app/models/.empty_directory +0 -0
  442. data/lib/rails/generators/rails/session_migration/USAGE +0 -8
  443. data/lib/rails/generators/rails/session_migration/session_migration_generator.rb +0 -8
  444. data/lib/rails/generators/test_unit/observer/observer_generator.rb +0 -13
  445. data/lib/rails/generators/test_unit/observer/templates/unit_test.rb +0 -9
  446. data/lib/rails/generators/test_unit/performance/performance_generator.rb +0 -13
  447. data/lib/rails/generators/test_unit/performance/templates/performance_test.rb +0 -12
  448. data/lib/rails/performance_test_help.rb +0 -3
  449. data/lib/rails/plugin.rb +0 -92
  450. data/lib/rails/script_rails_loader.rb +0 -29
@@ -1,947 +0,0 @@
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