rails 4.2.0.beta4 → 4.2.0.rc1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (230) hide show
  1. checksums.yaml +4 -4
  2. data/guides/Rakefile +3 -1
  3. data/guides/bug_report_templates/action_controller_gem.rb +2 -2
  4. data/guides/bug_report_templates/action_controller_master.rb +2 -2
  5. data/guides/source/3_0_release_notes.md +2 -2
  6. data/guides/source/4_1_release_notes.md +1 -1
  7. data/guides/source/4_2_release_notes.md +250 -225
  8. data/guides/source/_welcome.html.erb +1 -1
  9. data/guides/source/action_controller_overview.md +6 -6
  10. data/guides/source/action_mailer_basics.md +26 -11
  11. data/guides/source/action_view_overview.md +0 -1
  12. data/guides/source/active_job_basics.md +29 -23
  13. data/guides/source/active_record_postgresql.md +2 -7
  14. data/guides/source/active_record_validations.md +1 -0
  15. data/guides/source/active_support_core_extensions.md +1 -1
  16. data/guides/source/active_support_instrumentation.md +5 -5
  17. data/guides/source/asset_pipeline.md +2 -2
  18. data/guides/source/association_basics.md +0 -2
  19. data/guides/source/command_line.md +6 -15
  20. data/guides/source/configuring.md +4 -4
  21. data/guides/source/documents.yaml +0 -1
  22. data/guides/source/engines.md +26 -15
  23. data/guides/source/form_helpers.md +10 -2
  24. data/guides/source/getting_started.md +9 -9
  25. data/guides/source/rails_on_rack.md +2 -2
  26. data/guides/source/ruby_on_rails_guides_guidelines.md +1 -0
  27. data/guides/source/security.md +1 -1
  28. data/guides/source/testing.md +80 -13
  29. data/guides/source/upgrading_ruby_on_rails.md +23 -20
  30. metadata +29 -229
  31. data/guides/output/2_2_release_notes.html +0 -724
  32. data/guides/output/2_3_release_notes.html +0 -870
  33. data/guides/output/3_0_release_notes.html +0 -773
  34. data/guides/output/3_1_release_notes.html +0 -740
  35. data/guides/output/3_2_release_notes.html +0 -797
  36. data/guides/output/4_0_release_notes.html +0 -523
  37. data/guides/output/4_1_release_notes.html +0 -806
  38. data/guides/output/4_2_release_notes.html +0 -728
  39. data/guides/output/Gemfile +0 -6
  40. data/guides/output/_license.html +0 -226
  41. data/guides/output/_welcome.html +0 -233
  42. data/guides/output/action_controller_overview.html +0 -1335
  43. data/guides/output/action_mailer_basics.html +0 -928
  44. data/guides/output/action_view_overview.html +0 -1509
  45. data/guides/output/active_job_basics.html +0 -546
  46. data/guides/output/active_model_basics.html +0 -438
  47. data/guides/output/active_record_basics.html +0 -594
  48. data/guides/output/active_record_callbacks.html +0 -592
  49. data/guides/output/active_record_migrations.html +0 -1123
  50. data/guides/output/active_record_postgresql.html +0 -675
  51. data/guides/output/active_record_querying.html +0 -1796
  52. data/guides/output/active_record_validations.html +0 -1301
  53. data/guides/output/active_support_core_extensions.html +0 -3450
  54. data/guides/output/active_support_instrumentation.html +0 -1121
  55. data/guides/output/api_documentation_guidelines.html +0 -498
  56. data/guides/output/asset_pipeline.html +0 -1167
  57. data/guides/output/association_basics.html +0 -2086
  58. data/guides/output/caching_with_rails.html +0 -553
  59. data/guides/output/command_line.html +0 -791
  60. data/guides/output/configuring.html +0 -1055
  61. data/guides/output/contributing_to_ruby_on_rails.html +0 -657
  62. data/guides/output/credits.html +0 -284
  63. data/guides/output/debugging_rails_applications.html +0 -1014
  64. data/guides/output/development_dependencies_install.html +0 -478
  65. data/guides/output/engines.html +0 -1438
  66. data/guides/output/form_helpers.html +0 -1074
  67. data/guides/output/generators.html +0 -838
  68. data/guides/output/getting_started.html +0 -2092
  69. data/guides/output/i18n.html +0 -1198
  70. data/guides/output/images/akshaysurve.jpg +0 -0
  71. data/guides/output/images/belongs_to.png +0 -0
  72. data/guides/output/images/book_icon.gif +0 -0
  73. data/guides/output/images/bullet.gif +0 -0
  74. data/guides/output/images/chapters_icon.gif +0 -0
  75. data/guides/output/images/check_bullet.gif +0 -0
  76. data/guides/output/images/credits_pic_blank.gif +0 -0
  77. data/guides/output/images/csrf.png +0 -0
  78. data/guides/output/images/edge_badge.png +0 -0
  79. data/guides/output/images/favicon.ico +0 -0
  80. data/guides/output/images/feature_tile.gif +0 -0
  81. data/guides/output/images/footer_tile.gif +0 -0
  82. data/guides/output/images/fxn.png +0 -0
  83. data/guides/output/images/getting_started/article_with_comments.png +0 -0
  84. data/guides/output/images/getting_started/challenge.png +0 -0
  85. data/guides/output/images/getting_started/confirm_dialog.png +0 -0
  86. data/guides/output/images/getting_started/forbidden_attributes_for_new_article.png +0 -0
  87. data/guides/output/images/getting_started/forbidden_attributes_for_new_post.png +0 -0
  88. data/guides/output/images/getting_started/form_with_errors.png +0 -0
  89. data/guides/output/images/getting_started/index_action_with_edit_link.png +0 -0
  90. data/guides/output/images/getting_started/new_article.png +0 -0
  91. data/guides/output/images/getting_started/new_post.png +0 -0
  92. data/guides/output/images/getting_started/post_with_comments.png +0 -0
  93. data/guides/output/images/getting_started/rails_welcome.png +0 -0
  94. data/guides/output/images/getting_started/routing_error_no_controller.png +0 -0
  95. data/guides/output/images/getting_started/routing_error_no_route_matches.png +0 -0
  96. data/guides/output/images/getting_started/show_action_for_articles.png +0 -0
  97. data/guides/output/images/getting_started/show_action_for_posts.png +0 -0
  98. data/guides/output/images/getting_started/template_is_missing_articles_new.png +0 -0
  99. data/guides/output/images/getting_started/template_is_missing_posts_new.png +0 -0
  100. data/guides/output/images/getting_started/undefined_method_post_path.png +0 -0
  101. data/guides/output/images/getting_started/unknown_action_create_for_articles.png +0 -0
  102. data/guides/output/images/getting_started/unknown_action_create_for_posts.png +0 -0
  103. data/guides/output/images/getting_started/unknown_action_new_for_articles.png +0 -0
  104. data/guides/output/images/getting_started/unknown_action_new_for_posts.png +0 -0
  105. data/guides/output/images/grey_bullet.gif +0 -0
  106. data/guides/output/images/habtm.png +0 -0
  107. data/guides/output/images/has_many.png +0 -0
  108. data/guides/output/images/has_many_through.png +0 -0
  109. data/guides/output/images/has_one.png +0 -0
  110. data/guides/output/images/has_one_through.png +0 -0
  111. data/guides/output/images/header_backdrop.png +0 -0
  112. data/guides/output/images/header_tile.gif +0 -0
  113. data/guides/output/images/i18n/demo_html_safe.png +0 -0
  114. data/guides/output/images/i18n/demo_localized_pirate.png +0 -0
  115. data/guides/output/images/i18n/demo_translated_en.png +0 -0
  116. data/guides/output/images/i18n/demo_translated_pirate.png +0 -0
  117. data/guides/output/images/i18n/demo_translation_missing.png +0 -0
  118. data/guides/output/images/i18n/demo_untranslated.png +0 -0
  119. data/guides/output/images/icons/README +0 -5
  120. data/guides/output/images/icons/callouts/1.png +0 -0
  121. data/guides/output/images/icons/callouts/10.png +0 -0
  122. data/guides/output/images/icons/callouts/11.png +0 -0
  123. data/guides/output/images/icons/callouts/12.png +0 -0
  124. data/guides/output/images/icons/callouts/13.png +0 -0
  125. data/guides/output/images/icons/callouts/14.png +0 -0
  126. data/guides/output/images/icons/callouts/15.png +0 -0
  127. data/guides/output/images/icons/callouts/2.png +0 -0
  128. data/guides/output/images/icons/callouts/3.png +0 -0
  129. data/guides/output/images/icons/callouts/4.png +0 -0
  130. data/guides/output/images/icons/callouts/5.png +0 -0
  131. data/guides/output/images/icons/callouts/6.png +0 -0
  132. data/guides/output/images/icons/callouts/7.png +0 -0
  133. data/guides/output/images/icons/callouts/8.png +0 -0
  134. data/guides/output/images/icons/callouts/9.png +0 -0
  135. data/guides/output/images/icons/caution.png +0 -0
  136. data/guides/output/images/icons/example.png +0 -0
  137. data/guides/output/images/icons/home.png +0 -0
  138. data/guides/output/images/icons/important.png +0 -0
  139. data/guides/output/images/icons/next.png +0 -0
  140. data/guides/output/images/icons/note.png +0 -0
  141. data/guides/output/images/icons/prev.png +0 -0
  142. data/guides/output/images/icons/tip.png +0 -0
  143. data/guides/output/images/icons/up.png +0 -0
  144. data/guides/output/images/icons/warning.png +0 -0
  145. data/guides/output/images/nav_arrow.gif +0 -0
  146. data/guides/output/images/oscardelben.jpg +0 -0
  147. data/guides/output/images/polymorphic.png +0 -0
  148. data/guides/output/images/radar.png +0 -0
  149. data/guides/output/images/rails4_features.png +0 -0
  150. data/guides/output/images/rails_guides_kindle_cover.jpg +0 -0
  151. data/guides/output/images/rails_guides_logo.gif +0 -0
  152. data/guides/output/images/rails_logo_remix.gif +0 -0
  153. data/guides/output/images/session_fixation.png +0 -0
  154. data/guides/output/images/tab_grey.gif +0 -0
  155. data/guides/output/images/tab_info.gif +0 -0
  156. data/guides/output/images/tab_note.gif +0 -0
  157. data/guides/output/images/tab_red.gif +0 -0
  158. data/guides/output/images/tab_yellow.gif +0 -0
  159. data/guides/output/images/tab_yellow.png +0 -0
  160. data/guides/output/images/vijaydev.jpg +0 -0
  161. data/guides/output/index.html +0 -354
  162. data/guides/output/initialization.html +0 -876
  163. data/guides/output/javascripts/guides.js +0 -59
  164. data/guides/output/javascripts/jquery.min.js +0 -4
  165. data/guides/output/javascripts/responsive-tables.js +0 -43
  166. data/guides/output/javascripts/syntaxhighlighter/shBrushAS3.js +0 -59
  167. data/guides/output/javascripts/syntaxhighlighter/shBrushAppleScript.js +0 -75
  168. data/guides/output/javascripts/syntaxhighlighter/shBrushBash.js +0 -59
  169. data/guides/output/javascripts/syntaxhighlighter/shBrushCSharp.js +0 -65
  170. data/guides/output/javascripts/syntaxhighlighter/shBrushColdFusion.js +0 -100
  171. data/guides/output/javascripts/syntaxhighlighter/shBrushCpp.js +0 -97
  172. data/guides/output/javascripts/syntaxhighlighter/shBrushCss.js +0 -91
  173. data/guides/output/javascripts/syntaxhighlighter/shBrushDelphi.js +0 -55
  174. data/guides/output/javascripts/syntaxhighlighter/shBrushDiff.js +0 -41
  175. data/guides/output/javascripts/syntaxhighlighter/shBrushErlang.js +0 -52
  176. data/guides/output/javascripts/syntaxhighlighter/shBrushGroovy.js +0 -67
  177. data/guides/output/javascripts/syntaxhighlighter/shBrushJScript.js +0 -52
  178. data/guides/output/javascripts/syntaxhighlighter/shBrushJava.js +0 -57
  179. data/guides/output/javascripts/syntaxhighlighter/shBrushJavaFX.js +0 -58
  180. data/guides/output/javascripts/syntaxhighlighter/shBrushPerl.js +0 -72
  181. data/guides/output/javascripts/syntaxhighlighter/shBrushPhp.js +0 -88
  182. data/guides/output/javascripts/syntaxhighlighter/shBrushPlain.js +0 -33
  183. data/guides/output/javascripts/syntaxhighlighter/shBrushPowerShell.js +0 -74
  184. data/guides/output/javascripts/syntaxhighlighter/shBrushPython.js +0 -64
  185. data/guides/output/javascripts/syntaxhighlighter/shBrushRuby.js +0 -55
  186. data/guides/output/javascripts/syntaxhighlighter/shBrushSass.js +0 -94
  187. data/guides/output/javascripts/syntaxhighlighter/shBrushScala.js +0 -51
  188. data/guides/output/javascripts/syntaxhighlighter/shBrushSql.js +0 -66
  189. data/guides/output/javascripts/syntaxhighlighter/shBrushVb.js +0 -56
  190. data/guides/output/javascripts/syntaxhighlighter/shBrushXml.js +0 -69
  191. data/guides/output/javascripts/syntaxhighlighter/shCore.js +0 -17
  192. data/guides/output/layout.html +0 -448
  193. data/guides/output/layouts_and_rendering.html +0 -1541
  194. data/guides/output/maintenance_policy.html +0 -257
  195. data/guides/output/migrations.html +0 -1360
  196. data/guides/output/nested_model_forms.html +0 -412
  197. data/guides/output/plugins.html +0 -644
  198. data/guides/output/rails_application_templates.html +0 -450
  199. data/guides/output/rails_on_rack.html +0 -547
  200. data/guides/output/routing.html +0 -1631
  201. data/guides/output/ruby_on_rails_guides_guidelines.html +0 -329
  202. data/guides/output/security.html +0 -935
  203. data/guides/output/stylesheets/fixes.css +0 -16
  204. data/guides/output/stylesheets/kindle.css +0 -11
  205. data/guides/output/stylesheets/main.css +0 -713
  206. data/guides/output/stylesheets/print.css +0 -52
  207. data/guides/output/stylesheets/reset.css +0 -43
  208. data/guides/output/stylesheets/responsive-tables.css +0 -50
  209. data/guides/output/stylesheets/style.css +0 -13
  210. data/guides/output/stylesheets/syntaxhighlighter/shCore.css +0 -226
  211. data/guides/output/stylesheets/syntaxhighlighter/shCoreDefault.css +0 -328
  212. data/guides/output/stylesheets/syntaxhighlighter/shCoreDjango.css +0 -331
  213. data/guides/output/stylesheets/syntaxhighlighter/shCoreEclipse.css +0 -339
  214. data/guides/output/stylesheets/syntaxhighlighter/shCoreEmacs.css +0 -324
  215. data/guides/output/stylesheets/syntaxhighlighter/shCoreFadeToGrey.css +0 -328
  216. data/guides/output/stylesheets/syntaxhighlighter/shCoreMDUltra.css +0 -324
  217. data/guides/output/stylesheets/syntaxhighlighter/shCoreMidnight.css +0 -324
  218. data/guides/output/stylesheets/syntaxhighlighter/shCoreRDark.css +0 -324
  219. data/guides/output/stylesheets/syntaxhighlighter/shThemeDefault.css +0 -117
  220. data/guides/output/stylesheets/syntaxhighlighter/shThemeDjango.css +0 -120
  221. data/guides/output/stylesheets/syntaxhighlighter/shThemeEclipse.css +0 -128
  222. data/guides/output/stylesheets/syntaxhighlighter/shThemeEmacs.css +0 -113
  223. data/guides/output/stylesheets/syntaxhighlighter/shThemeFadeToGrey.css +0 -117
  224. data/guides/output/stylesheets/syntaxhighlighter/shThemeMDUltra.css +0 -113
  225. data/guides/output/stylesheets/syntaxhighlighter/shThemeMidnight.css +0 -113
  226. data/guides/output/stylesheets/syntaxhighlighter/shThemeRDark.css +0 -113
  227. data/guides/output/stylesheets/syntaxhighlighter/shThemeRailsGuides.css +0 -116
  228. data/guides/output/testing.html +0 -1350
  229. data/guides/output/upgrading_ruby_on_rails.html +0 -1135
  230. data/guides/output/working_with_javascript_in_rails.html +0 -587
@@ -1,113 +0,0 @@
1
- /**
2
- * SyntaxHighlighter
3
- * http://alexgorbatchev.com/SyntaxHighlighter
4
- *
5
- * SyntaxHighlighter is donationware. If you are using it, please donate.
6
- * http://alexgorbatchev.com/SyntaxHighlighter/donate.html
7
- *
8
- * @version
9
- * 3.0.83 (July 02 2010)
10
- *
11
- * @copyright
12
- * Copyright (C) 2004-2010 Alex Gorbatchev.
13
- *
14
- * @license
15
- * Dual licensed under the MIT and GPL licenses.
16
- */
17
- .syntaxhighlighter {
18
- background-color: #0f192a !important;
19
- }
20
- .syntaxhighlighter .line.alt1 {
21
- background-color: #0f192a !important;
22
- }
23
- .syntaxhighlighter .line.alt2 {
24
- background-color: #0f192a !important;
25
- }
26
- .syntaxhighlighter .line.highlighted.alt1, .syntaxhighlighter .line.highlighted.alt2 {
27
- background-color: #253e5a !important;
28
- }
29
- .syntaxhighlighter .line.highlighted.number {
30
- color: #38566f !important;
31
- }
32
- .syntaxhighlighter table caption {
33
- color: #d1edff !important;
34
- }
35
- .syntaxhighlighter .gutter {
36
- color: #afafaf !important;
37
- }
38
- .syntaxhighlighter .gutter .line {
39
- border-right: 3px solid #435a5f !important;
40
- }
41
- .syntaxhighlighter .gutter .line.highlighted {
42
- background-color: #435a5f !important;
43
- color: #0f192a !important;
44
- }
45
- .syntaxhighlighter.printing .line .content {
46
- border: none !important;
47
- }
48
- .syntaxhighlighter.collapsed {
49
- overflow: visible !important;
50
- }
51
- .syntaxhighlighter.collapsed .toolbar {
52
- color: #428bdd !important;
53
- background: black !important;
54
- border: 1px solid #435a5f !important;
55
- }
56
- .syntaxhighlighter.collapsed .toolbar a {
57
- color: #428bdd !important;
58
- }
59
- .syntaxhighlighter.collapsed .toolbar a:hover {
60
- color: #1dc116 !important;
61
- }
62
- .syntaxhighlighter .toolbar {
63
- color: #d1edff !important;
64
- background: #435a5f !important;
65
- border: none !important;
66
- }
67
- .syntaxhighlighter .toolbar a {
68
- color: #d1edff !important;
69
- }
70
- .syntaxhighlighter .toolbar a:hover {
71
- color: #8aa6c1 !important;
72
- }
73
- .syntaxhighlighter .plain, .syntaxhighlighter .plain a {
74
- color: #d1edff !important;
75
- }
76
- .syntaxhighlighter .comments, .syntaxhighlighter .comments a {
77
- color: #428bdd !important;
78
- }
79
- .syntaxhighlighter .string, .syntaxhighlighter .string a {
80
- color: #1dc116 !important;
81
- }
82
- .syntaxhighlighter .keyword {
83
- color: #b43d3d !important;
84
- }
85
- .syntaxhighlighter .preprocessor {
86
- color: #8aa6c1 !important;
87
- }
88
- .syntaxhighlighter .variable {
89
- color: #ffaa3e !important;
90
- }
91
- .syntaxhighlighter .value {
92
- color: #f7e741 !important;
93
- }
94
- .syntaxhighlighter .functions {
95
- color: #ffaa3e !important;
96
- }
97
- .syntaxhighlighter .constants {
98
- color: #e0e8ff !important;
99
- }
100
- .syntaxhighlighter .script {
101
- font-weight: bold !important;
102
- color: #b43d3d !important;
103
- background-color: none !important;
104
- }
105
- .syntaxhighlighter .color1, .syntaxhighlighter .color1 a {
106
- color: #f8bb00 !important;
107
- }
108
- .syntaxhighlighter .color2, .syntaxhighlighter .color2 a {
109
- color: white !important;
110
- }
111
- .syntaxhighlighter .color3, .syntaxhighlighter .color3 a {
112
- color: #ffaa3e !important;
113
- }
@@ -1,113 +0,0 @@
1
- /**
2
- * SyntaxHighlighter
3
- * http://alexgorbatchev.com/SyntaxHighlighter
4
- *
5
- * SyntaxHighlighter is donationware. If you are using it, please donate.
6
- * http://alexgorbatchev.com/SyntaxHighlighter/donate.html
7
- *
8
- * @version
9
- * 3.0.83 (July 02 2010)
10
- *
11
- * @copyright
12
- * Copyright (C) 2004-2010 Alex Gorbatchev.
13
- *
14
- * @license
15
- * Dual licensed under the MIT and GPL licenses.
16
- */
17
- .syntaxhighlighter {
18
- background-color: #1b2426 !important;
19
- }
20
- .syntaxhighlighter .line.alt1 {
21
- background-color: #1b2426 !important;
22
- }
23
- .syntaxhighlighter .line.alt2 {
24
- background-color: #1b2426 !important;
25
- }
26
- .syntaxhighlighter .line.highlighted.alt1, .syntaxhighlighter .line.highlighted.alt2 {
27
- background-color: #323e41 !important;
28
- }
29
- .syntaxhighlighter .line.highlighted.number {
30
- color: #b9bdb6 !important;
31
- }
32
- .syntaxhighlighter table caption {
33
- color: #b9bdb6 !important;
34
- }
35
- .syntaxhighlighter .gutter {
36
- color: #afafaf !important;
37
- }
38
- .syntaxhighlighter .gutter .line {
39
- border-right: 3px solid #435a5f !important;
40
- }
41
- .syntaxhighlighter .gutter .line.highlighted {
42
- background-color: #435a5f !important;
43
- color: #1b2426 !important;
44
- }
45
- .syntaxhighlighter.printing .line .content {
46
- border: none !important;
47
- }
48
- .syntaxhighlighter.collapsed {
49
- overflow: visible !important;
50
- }
51
- .syntaxhighlighter.collapsed .toolbar {
52
- color: #5ba1cf !important;
53
- background: black !important;
54
- border: 1px solid #435a5f !important;
55
- }
56
- .syntaxhighlighter.collapsed .toolbar a {
57
- color: #5ba1cf !important;
58
- }
59
- .syntaxhighlighter.collapsed .toolbar a:hover {
60
- color: #5ce638 !important;
61
- }
62
- .syntaxhighlighter .toolbar {
63
- color: white !important;
64
- background: #435a5f !important;
65
- border: none !important;
66
- }
67
- .syntaxhighlighter .toolbar a {
68
- color: white !important;
69
- }
70
- .syntaxhighlighter .toolbar a:hover {
71
- color: #e0e8ff !important;
72
- }
73
- .syntaxhighlighter .plain, .syntaxhighlighter .plain a {
74
- color: #b9bdb6 !important;
75
- }
76
- .syntaxhighlighter .comments, .syntaxhighlighter .comments a {
77
- color: #878a85 !important;
78
- }
79
- .syntaxhighlighter .string, .syntaxhighlighter .string a {
80
- color: #5ce638 !important;
81
- }
82
- .syntaxhighlighter .keyword {
83
- color: #5ba1cf !important;
84
- }
85
- .syntaxhighlighter .preprocessor {
86
- color: #435a5f !important;
87
- }
88
- .syntaxhighlighter .variable {
89
- color: #ffaa3e !important;
90
- }
91
- .syntaxhighlighter .value {
92
- color: #009900 !important;
93
- }
94
- .syntaxhighlighter .functions {
95
- color: #ffaa3e !important;
96
- }
97
- .syntaxhighlighter .constants {
98
- color: #e0e8ff !important;
99
- }
100
- .syntaxhighlighter .script {
101
- font-weight: bold !important;
102
- color: #5ba1cf !important;
103
- background-color: none !important;
104
- }
105
- .syntaxhighlighter .color1, .syntaxhighlighter .color1 a {
106
- color: #e0e8ff !important;
107
- }
108
- .syntaxhighlighter .color2, .syntaxhighlighter .color2 a {
109
- color: white !important;
110
- }
111
- .syntaxhighlighter .color3, .syntaxhighlighter .color3 a {
112
- color: #ffaa3e !important;
113
- }
@@ -1,116 +0,0 @@
1
- /**
2
- * Theme by fxn, took shThemeEclipse.css as starting point.
3
- */
4
- .syntaxhighlighter {
5
- background-color: #eee !important;
6
- font-family: "Anonymous Pro", "Inconsolata", "Menlo", "Consolas", "Bitstream Vera Sans Mono", "Courier New", monospace !important;
7
- overflow-y: hidden !important;
8
- overflow-x: auto !important;
9
- }
10
- .syntaxhighlighter .line.alt1 {
11
- background-color: #eee !important;
12
- }
13
- .syntaxhighlighter .line.alt2 {
14
- background-color: #eee !important;
15
- }
16
- .syntaxhighlighter .line.highlighted.alt1, .syntaxhighlighter .line.highlighted.alt2 {
17
- background-color: #c3defe !important;
18
- }
19
- .syntaxhighlighter .line.highlighted.number {
20
- color: #eee !important;
21
- }
22
- .syntaxhighlighter table caption {
23
- color: #222 !important;
24
- }
25
- .syntaxhighlighter .gutter {
26
- color: #787878 !important;
27
- }
28
- .syntaxhighlighter .gutter .line {
29
- border-right: 3px solid #d4d0c8 !important;
30
- }
31
- .syntaxhighlighter .gutter .line.highlighted {
32
- background-color: #d4d0c8 !important;
33
- color: #eee !important;
34
- }
35
- .syntaxhighlighter.printing .line .content {
36
- border: none !important;
37
- }
38
- .syntaxhighlighter.collapsed {
39
- overflow: visible !important;
40
- }
41
- .syntaxhighlighter.collapsed .toolbar {
42
- color: #3f5fbf !important;
43
- background: #eee !important;
44
- border: 1px solid #d4d0c8 !important;
45
- }
46
- .syntaxhighlighter.collapsed .toolbar a {
47
- color: #3f5fbf !important;
48
- }
49
- .syntaxhighlighter.collapsed .toolbar a:hover {
50
- color: #aa7700 !important;
51
- }
52
- .syntaxhighlighter .toolbar {
53
- color: #a0a0a0 !important;
54
- background: #d4d0c8 !important;
55
- border: none !important;
56
- }
57
- .syntaxhighlighter .toolbar a {
58
- color: #a0a0a0 !important;
59
- }
60
- .syntaxhighlighter .toolbar a:hover {
61
- color: red !important;
62
- }
63
- .syntaxhighlighter .plain, .syntaxhighlighter .plain a {
64
- color: #222 !important;
65
- }
66
- .syntaxhighlighter .comments, .syntaxhighlighter .comments a {
67
- color: #708090 !important;
68
- }
69
- .syntaxhighlighter .string, .syntaxhighlighter .string a {
70
- font-style: italic !important;
71
- color: #6588A8 !important;
72
- }
73
- .syntaxhighlighter .keyword {
74
- color: #64434d !important;
75
- }
76
- .syntaxhighlighter .preprocessor {
77
- color: #646464 !important;
78
- }
79
- .syntaxhighlighter .variable {
80
- color: #222 !important;
81
- }
82
- .syntaxhighlighter .value {
83
- color: #009900 !important;
84
- }
85
- .syntaxhighlighter .functions {
86
- color: #ff1493 !important;
87
- }
88
- .syntaxhighlighter .constants {
89
- color: #0066cc !important;
90
- }
91
- .syntaxhighlighter .script {
92
- color: #222 !important;
93
- background-color: none !important;
94
- }
95
- .syntaxhighlighter .color1, .syntaxhighlighter .color1 a {
96
- color: gray !important;
97
- }
98
- .syntaxhighlighter .color2, .syntaxhighlighter .color2 a {
99
- color: #222 !important;
100
- font-weight: bold !important;
101
- }
102
- .syntaxhighlighter .color3, .syntaxhighlighter .color3 a {
103
- color: red !important;
104
- }
105
-
106
- .syntaxhighlighter .xml .keyword {
107
- color: #64434d !important;
108
- font-weight: normal !important;
109
- }
110
- .syntaxhighlighter .xml .color1, .syntaxhighlighter .xml .color1 a {
111
- color: #7f007f !important;
112
- }
113
- .syntaxhighlighter .xml .string {
114
- font-style: italic !important;
115
- color: #6588A8 !important;
116
- }
@@ -1,1350 +0,0 @@
1
- <!DOCTYPE html>
2
-
3
- <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
4
- <head>
5
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
6
- <meta name="viewport" content="width=device-width, initial-scale=1"/>
7
-
8
- <title>A Guide to Testing Rails Applications — Ruby on Rails Guides</title>
9
- <link rel="stylesheet" type="text/css" href="stylesheets/style.css" />
10
- <link rel="stylesheet" type="text/css" href="stylesheets/print.css" media="print" />
11
-
12
- <link rel="stylesheet" type="text/css" href="stylesheets/syntaxhighlighter/shCore.css" />
13
- <link rel="stylesheet" type="text/css" href="stylesheets/syntaxhighlighter/shThemeRailsGuides.css" />
14
-
15
- <link rel="stylesheet" type="text/css" href="stylesheets/fixes.css" />
16
-
17
- <link href="images/favicon.ico" rel="shortcut icon" type="image/x-icon" />
18
- </head>
19
- <body class="guide">
20
- <div id="topNav">
21
- <div class="wrapper">
22
- <strong class="more-info-label">More at <a href="http://rubyonrails.org/">rubyonrails.org:</a> </strong>
23
- <span class="red-button more-info-button">
24
- More Ruby on Rails
25
- </span>
26
- <ul class="more-info-links s-hidden">
27
- <li class="more-info"><a href="http://rubyonrails.org/">Overview</a></li>
28
- <li class="more-info"><a href="http://rubyonrails.org/download">Download</a></li>
29
- <li class="more-info"><a href="http://rubyonrails.org/deploy">Deploy</a></li>
30
- <li class="more-info"><a href="https://github.com/rails/rails">Code</a></li>
31
- <li class="more-info"><a href="http://rubyonrails.org/screencasts">Screencasts</a></li>
32
- <li class="more-info"><a href="http://rubyonrails.org/documentation">Documentation</a></li>
33
- <li class="more-info"><a href="http://rubyonrails.org/community">Community</a></li>
34
- <li class="more-info"><a href="http://weblog.rubyonrails.org/">Blog</a></li>
35
- </ul>
36
- </div>
37
- </div>
38
- <div id="header">
39
- <div class="wrapper clearfix">
40
- <h1><a href="index.html" title="Return to home page">Guides.rubyonrails.org</a></h1>
41
- <ul class="nav">
42
- <li><a class="nav-item" href="index.html">Home</a></li>
43
- <li class="guides-index guides-index-large">
44
- <a href="index.html" id="guidesMenu" class="guides-index-item nav-item">Guides Index</a>
45
- <div id="guides" class="clearfix" style="display: none;">
46
- <hr />
47
- <dl class="L">
48
- <dt>Start Here</dt>
49
- <dd><a href="getting_started.html">Getting Started with Rails</a></dd>
50
- <dt>Models</dt>
51
- <dd><a href="active_record_basics.html">Active Record Basics</a></dd>
52
- <dd><a href="active_record_migrations.html">Active Record Migrations</a></dd>
53
- <dd><a href="active_record_validations.html">Active Record Validations</a></dd>
54
- <dd><a href="active_record_callbacks.html">Active Record Callbacks</a></dd>
55
- <dd><a href="association_basics.html">Active Record Associations</a></dd>
56
- <dd><a href="active_record_querying.html">Active Record Query Interface</a></dd>
57
- <dt>Views</dt>
58
- <dd><a href="layouts_and_rendering.html">Layouts and Rendering in Rails</a></dd>
59
- <dd><a href="form_helpers.html">Action View Form Helpers</a></dd>
60
- <dt>Controllers</dt>
61
- <dd><a href="action_controller_overview.html">Action Controller Overview</a></dd>
62
- <dd><a href="routing.html">Rails Routing from the Outside In</a></dd>
63
- </dl>
64
- <dl class="R">
65
- <dt>Digging Deeper</dt>
66
- <dd><a href="active_support_core_extensions.html">Active Support Core Extensions</a></dd>
67
- <dd><a href="i18n.html">Rails Internationalization API</a></dd>
68
- <dd><a href="action_mailer_basics.html">Action Mailer Basics</a></dd>
69
- <dd><a href="active_job_basics.html">Active Job Basics</a></dd>
70
- <dd><a href="security.html">Securing Rails Applications</a></dd>
71
- <dd><a href="debugging_rails_applications.html">Debugging Rails Applications</a></dd>
72
- <dd><a href="configuring.html">Configuring Rails Applications</a></dd>
73
- <dd><a href="command_line.html">Rails Command Line Tools and Rake Tasks</a></dd>
74
- <dd><a href="asset_pipeline.html">Asset Pipeline</a></dd>
75
- <dd><a href="working_with_javascript_in_rails.html">Working with JavaScript in Rails</a></dd>
76
- <dt>Extending Rails</dt>
77
- <dd><a href="rails_on_rack.html">Rails on Rack</a></dd>
78
- <dd><a href="generators.html">Creating and Customizing Rails Generators</a></dd>
79
- <dt>Contributing to Ruby on Rails</dt>
80
- <dd><a href="contributing_to_ruby_on_rails.html">Contributing to Ruby on Rails</a></dd>
81
- <dd><a href="api_documentation_guidelines.html">API Documentation Guidelines</a></dd>
82
- <dd><a href="ruby_on_rails_guides_guidelines.html">Ruby on Rails Guides Guidelines</a></dd>
83
- <dt>Maintenance Policy</dt>
84
- <dd><a href="maintenance_policy.html">Maintenance Policy</a></dd>
85
- <dt>Release Notes</dt>
86
- <dd><a href="upgrading_ruby_on_rails.html">Upgrading Ruby on Rails</a></dd>
87
- <dd><a href="4_1_release_notes.html">Ruby on Rails 4.1 Release Notes</a></dd>
88
- <dd><a href="4_0_release_notes.html">Ruby on Rails 4.0 Release Notes</a></dd>
89
- <dd><a href="3_2_release_notes.html">Ruby on Rails 3.2 Release Notes</a></dd>
90
- <dd><a href="3_1_release_notes.html">Ruby on Rails 3.1 Release Notes</a></dd>
91
- <dd><a href="3_0_release_notes.html">Ruby on Rails 3.0 Release Notes</a></dd>
92
- <dd><a href="2_3_release_notes.html">Ruby on Rails 2.3 Release Notes</a></dd>
93
- <dd><a href="2_2_release_notes.html">Ruby on Rails 2.2 Release Notes</a></dd>
94
- </dl>
95
- </div>
96
- </li>
97
- <li><a class="nav-item" href="contributing_to_ruby_on_rails.html">Contribute</a></li>
98
- <li><a class="nav-item" href="credits.html">Credits</a></li>
99
- <li class="guides-index guides-index-small">
100
- <select class="guides-index-item nav-item">
101
- <option value="index.html">Guides Index</option>
102
- <optgroup label="Start Here">
103
- <option value="getting_started.html">Getting Started with Rails</option>
104
- </optgroup>
105
- <optgroup label="Models">
106
- <option value="active_record_basics.html">Active Record Basics</option>
107
- <option value="active_record_migrations.html">Active Record Migrations</option>
108
- <option value="active_record_validations.html">Active Record Validations</option>
109
- <option value="active_record_callbacks.html">Active Record Callbacks</option>
110
- <option value="association_basics.html">Active Record Associations</option>
111
- <option value="active_record_querying.html">Active Record Query Interface</option>
112
- </optgroup>
113
- <optgroup label="Views">
114
- <option value="layouts_and_rendering.html">Layouts and Rendering in Rails</option>
115
- <option value="form_helpers.html">Action View Form Helpers</option>
116
- </optgroup>
117
- <optgroup label="Controllers">
118
- <option value="action_controller_overview.html">Action Controller Overview</option>
119
- <option value="routing.html">Rails Routing from the Outside In</option>
120
- </optgroup>
121
- <optgroup label="Digging Deeper">
122
- <option value="active_support_core_extensions.html">Active Support Core Extensions</option>
123
- <option value="i18n.html">Rails Internationalization API</option>
124
- <option value="action_mailer_basics.html">Action Mailer Basics</option>
125
- <option value="active_job_basics.html">Active Job Basics</option>
126
- <option value="security.html">Securing Rails Applications</option>
127
- <option value="debugging_rails_applications.html">Debugging Rails Applications</option>
128
- <option value="configuring.html">Configuring Rails Applications</option>
129
- <option value="command_line.html">Rails Command Line Tools and Rake Tasks</option>
130
- <option value="asset_pipeline.html">Asset Pipeline</option>
131
- <option value="working_with_javascript_in_rails.html">Working with JavaScript in Rails</option>
132
- </optgroup>
133
- <optgroup label="Extending Rails">
134
- <option value="rails_on_rack.html">Rails on Rack</option>
135
- <option value="generators.html">Creating and Customizing Rails Generators</option>
136
- </optgroup>
137
- <optgroup label="Contributing to Ruby on Rails">
138
- <option value="contributing_to_ruby_on_rails.html">Contributing to Ruby on Rails</option>
139
- <option value="api_documentation_guidelines.html">API Documentation Guidelines</option>
140
- <option value="ruby_on_rails_guides_guidelines.html">Ruby on Rails Guides Guidelines</option>
141
- </optgroup>
142
- <optgroup label="Maintenance Policy">
143
- <option value="maintenance_policy.html">Maintenance Policy</option>
144
- </optgroup>
145
- <optgroup label="Release Notes">
146
- <option value="upgrading_ruby_on_rails.html">Upgrading Ruby on Rails</option>
147
- <option value="4_1_release_notes.html">Ruby on Rails 4.1 Release Notes</option>
148
- <option value="4_0_release_notes.html">Ruby on Rails 4.0 Release Notes</option>
149
- <option value="3_2_release_notes.html">Ruby on Rails 3.2 Release Notes</option>
150
- <option value="3_1_release_notes.html">Ruby on Rails 3.1 Release Notes</option>
151
- <option value="3_0_release_notes.html">Ruby on Rails 3.0 Release Notes</option>
152
- <option value="2_3_release_notes.html">Ruby on Rails 2.3 Release Notes</option>
153
- <option value="2_2_release_notes.html">Ruby on Rails 2.2 Release Notes</option>
154
- </optgroup>
155
- </select>
156
- </li>
157
- </ul>
158
- </div>
159
- </div>
160
- <hr class="hide" />
161
-
162
- <div id="feature">
163
- <div class="wrapper">
164
- <h2>A Guide to Testing Rails Applications</h2><p>This guide covers built-in mechanisms in Rails for testing your application.</p><p>After reading this guide, you will know:</p>
165
- <ul>
166
- <li>Rails testing terminology.</li>
167
- <li>How to write unit, functional, and integration tests for your application.</li>
168
- <li>Other popular testing approaches and plugins.</li>
169
- </ul>
170
-
171
-
172
- <div id="subCol">
173
- <h3 class="chapter"><img src="images/chapters_icon.gif" alt="" />Chapters</h3>
174
- <ol class="chapters">
175
- <li><a href="#why-write-tests-for-your-rails-applications-questionmark">Why Write Tests for your Rails Applications?</a></li>
176
- <li>
177
- <a href="#introduction-to-testing">Introduction to Testing</a>
178
-
179
- <ul>
180
- <li><a href="#the-test-environment">The Test Environment</a></li>
181
- <li><a href="#rails-sets-up-for-testing-from-the-word-go">Rails Sets up for Testing from the Word Go</a></li>
182
- <li><a href="#the-low-down-on-fixtures">The Low-Down on Fixtures</a></li>
183
- </ul>
184
- </li>
185
- <li>
186
- <a href="#unit-testing-your-models">Unit Testing your Models</a>
187
-
188
- <ul>
189
- <li><a href="#maintaining-the-test-database-schema">Maintaining the test database schema</a></li>
190
- <li><a href="#running-tests">Running Tests</a></li>
191
- <li><a href="#what-to-include-in-your-unit-tests">What to Include in Your Unit Tests</a></li>
192
- <li><a href="#available-assertions">Available Assertions</a></li>
193
- <li><a href="#rails-specific-assertions">Rails Specific Assertions</a></li>
194
- </ul>
195
- </li>
196
- <li>
197
- <a href="#functional-tests-for-your-controllers">Functional Tests for Your Controllers</a>
198
-
199
- <ul>
200
- <li><a href="#what-to-include-in-your-functional-tests">What to Include in your Functional Tests</a></li>
201
- <li><a href="#available-request-types-for-functional-tests">Available Request Types for Functional Tests</a></li>
202
- <li><a href="#the-four-hashes-of-the-apocalypse">The Four Hashes of the Apocalypse</a></li>
203
- <li><a href="#instance-variables-available">Instance Variables Available</a></li>
204
- <li><a href="#setting-headers-and-cgi-variables">Setting Headers and CGI variables</a></li>
205
- <li><a href="#testing-templates-and-layouts">Testing Templates and Layouts</a></li>
206
- <li><a href="#a-fuller-functional-test-example">A Fuller Functional Test Example</a></li>
207
- <li><a href="#testing-views">Testing Views</a></li>
208
- </ul>
209
- </li>
210
- <li>
211
- <a href="#integration-testing">Integration Testing</a>
212
-
213
- <ul>
214
- <li><a href="#helpers-available-for-integration-tests">Helpers Available for Integration Tests</a></li>
215
- <li><a href="#integration-testing-examples">Integration Testing Examples</a></li>
216
- </ul>
217
- </li>
218
- <li><a href="#rake-tasks-for-running-your-tests">Rake Tasks for Running your Tests</a></li>
219
- <li><a href="#brief-note-about-minitest">Brief Note About <code>Minitest</code></a></li>
220
- <li><a href="#setup-and-teardown">Setup and Teardown</a></li>
221
- <li><a href="#testing-routes">Testing Routes</a></li>
222
- <li>
223
- <a href="#testing-your-mailers">Testing Your Mailers</a>
224
-
225
- <ul>
226
- <li><a href="#keeping-the-postman-in-check">Keeping the Postman in Check</a></li>
227
- <li><a href="#unit-testing">Unit Testing</a></li>
228
- <li><a href="#functional-testing">Functional Testing</a></li>
229
- </ul>
230
- </li>
231
- <li><a href="#testing-helpers">Testing helpers</a></li>
232
- <li><a href="#other-testing-approaches">Other Testing Approaches</a></li>
233
- </ol>
234
-
235
- </div>
236
-
237
- </div>
238
- </div>
239
-
240
- <div id="container">
241
- <div class="wrapper">
242
- <div id="mainCol">
243
- <h3 id="why-write-tests-for-your-rails-applications-questionmark">1 Why Write Tests for your Rails Applications?</h3><p>Rails makes it super easy to write your tests. It starts by producing skeleton test code while you are creating your models and controllers.</p><p>By simply running your Rails tests you can ensure your code adheres to the desired functionality even after some major code refactoring.</p><p>Rails tests can also simulate browser requests and thus you can test your application's response without having to test it through your browser.</p><h3 id="introduction-to-testing">2 Introduction to Testing</h3><p>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.</p><h4 id="the-test-environment">2.1 The Test Environment</h4><p>By default, every Rails application has three environments: development, test, and production. The database for each one of them is configured in <code>config/database.yml</code>.</p><p>A dedicated test database allows you to set up and interact with test data in isolation. Tests can mangle test data with confidence, that won't touch the data in the development or production databases.</p><h4 id="rails-sets-up-for-testing-from-the-word-go">2.2 Rails Sets up for Testing from the Word Go</h4><p>Rails creates a <code>test</code> folder for you as soon as you create a Rails project using <code>rails new</code> <em>application_name</em>. If you list the contents of this folder then you shall see:</p><div class="code_container">
244
- <pre class="brush: plain; gutter: false; toolbar: false">
245
- $ ls -F test
246
- controllers/ helpers/ mailers/ test_helper.rb
247
- fixtures/ integration/ models/
248
-
249
- </pre>
250
- </div>
251
- <p>The <code>models</code> directory is meant to hold tests for your models, the <code>controllers</code> directory is meant to hold tests for your controllers and the <code>integration</code> directory is meant to hold tests that involve any number of controllers interacting.</p><p>Fixtures are a way of organizing test data; they reside in the <code>fixtures</code> folder.</p><p>The <code>test_helper.rb</code> file holds the default configuration for your tests.</p><h4 id="the-low-down-on-fixtures">2.3 The Low-Down on Fixtures</h4><p>For good tests, you'll need to give some thought to setting up test data.
252
- In Rails, you can handle this by defining and customizing fixtures.
253
- You can find comprehensive documentation in the <a href="http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html">fixture api documentation</a>.</p><h5 id="what-are-fixtures-questionmark">2.3.1 What Are Fixtures?</h5><p><em>Fixtures</em> 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 written in YAML. There is one file per model.</p><p>You'll find fixtures under your <code>test/fixtures</code> directory. When you run <code>rails generate model</code> to create a new model fixture stubs will be automatically created and placed in this directory.</p><h5 id="yaml">2.3.2 YAML</h5><p>YAML-formatted fixtures are a very human-friendly way to describe your sample data. These types of fixtures have the <strong>.yml</strong> file extension (as in <code>users.yml</code>).</p><p>Here's a sample YAML fixture file:</p><div class="code_container">
254
- <pre class="brush: plain; gutter: false; toolbar: false">
255
- # lo &amp; behold! I am a YAML comment!
256
- david:
257
- name: David Heinemeier Hansson
258
- birthday: 1979-10-15
259
- profession: Systems development
260
-
261
- steve:
262
- name: Steve Ross Kellock
263
- birthday: 1974-09-27
264
- profession: guy with keyboard
265
-
266
- </pre>
267
- </div>
268
- <p>Each fixture is given a name followed by an indented list of colon-separated key/value pairs. Records are typically separated by a blank space. You can place comments in a fixture file by using the # character in the first column. Keys which resemble YAML keywords such as 'yes' and 'no' are quoted so that the YAML Parser correctly interprets them.</p><p>If you are working with <a href="/association_basics.html">associations</a>, you can simply
269
- define a reference node between two different fixtures. Here's an example with
270
- a belongs_to/has_many association:</p><div class="code_container">
271
- <pre class="brush: plain; gutter: false; toolbar: false">
272
- # In fixtures/categories.yml
273
- about:
274
- name: About
275
-
276
- # In fixtures/articles.yml
277
- one:
278
- title: Welcome to Rails!
279
- body: Hello world!
280
- category: about
281
-
282
- </pre>
283
- </div>
284
- <p>Note: For associations to reference one another by name, you cannot specify the <code>id:</code>
285
- attribute on the fixtures. Rails will auto assign a primary key to be consistent between
286
- runs. If you manually specify an <code>id:</code> attribute, this behavior will not work. For more
287
- information on this assocation behavior please read the
288
- <a href="http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html">fixture api documentation</a>.</p><h5 id="erb'in-it-up">2.3.3 ERB'in It Up</h5><p>ERB allows you to embed Ruby code within templates. The YAML fixture format is pre-processed with ERB when Rails loads fixtures. This allows you to use Ruby to help you generate some sample data. For example, the following code generates a thousand users:</p><div class="code_container">
289
- <pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
290
- &lt;% 1000.times do |n| %&gt;
291
- user_&lt;%= n %&gt;:
292
- username: &lt;%= "user#{n}" %&gt;
293
- email: &lt;%= "user#{n}@example.com" %&gt;
294
- &lt;% end %&gt;
295
-
296
- </pre>
297
- </div>
298
- <h5 id="fixtures-in-action">2.3.4 Fixtures in Action</h5><p>Rails by default automatically loads all fixtures from the <code>test/fixtures</code> folder for your models and controllers test. Loading involves three steps:</p>
299
- <ul>
300
- <li>Remove any existing data from the table corresponding to the fixture</li>
301
- <li>Load the fixture data into the table</li>
302
- <li>Dump the fixture data into a variable in case you want to access it directly</li>
303
- </ul>
304
- <h5 id="fixtures-are-active-record-objects">2.3.5 Fixtures are Active Record objects</h5><p>Fixtures are instances of Active Record. As mentioned in point #3 above, you can access the object directly because it is automatically setup as a local variable of the test case. For example:</p><div class="code_container">
305
- <pre class="brush: ruby; gutter: false; toolbar: false">
306
- # this will return the User object for the fixture named david
307
- users(:david)
308
-
309
- # this will return the property for david called id
310
- users(:david).id
311
-
312
- # one can also access methods available on the User class
313
- email(david.girlfriend.email, david.location_tonight)
314
-
315
- </pre>
316
- </div>
317
- <h3 id="unit-testing-your-models">3 Unit Testing your Models</h3><p>In Rails, models tests are what you write to test your models.</p><p>For this guide we will be using Rails <em>scaffolding</em>. 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. We will be using examples from this generated code and will be supplementing it with additional examples where necessary.</p><div class="note"><p>For more information on Rails <em>scaffolding</em>, refer to <a href="getting_started.html">Getting Started with Rails</a></p></div><p>When you use <code>rails generate scaffold</code>, for a resource among other things it creates a test stub in the <code>test/models</code> folder:</p><div class="code_container">
318
- <pre class="brush: plain; gutter: false; toolbar: false">
319
- $ bin/rails generate scaffold article title:string body:text
320
- ...
321
- create app/models/article.rb
322
- create test/models/article_test.rb
323
- create test/fixtures/articles.yml
324
- ...
325
-
326
- </pre>
327
- </div>
328
- <p>The default test stub in <code>test/models/article_test.rb</code> looks like this:</p><div class="code_container">
329
- <pre class="brush: ruby; gutter: false; toolbar: false">
330
- require 'test_helper'
331
-
332
- class ArticleTest &lt; ActiveSupport::TestCase
333
- # test "the truth" do
334
- # assert true
335
- # end
336
- end
337
-
338
- </pre>
339
- </div>
340
- <p>A line by line examination of this file will help get you oriented to Rails testing code and terminology.</p><div class="code_container">
341
- <pre class="brush: ruby; gutter: false; toolbar: false">
342
- require 'test_helper'
343
-
344
- </pre>
345
- </div>
346
- <p>As you know by now, <code>test_helper.rb</code> 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.</p><div class="code_container">
347
- <pre class="brush: ruby; gutter: false; toolbar: false">
348
- class ArticleTest &lt; ActiveSupport::TestCase
349
-
350
- </pre>
351
- </div>
352
- <p>The <code>ArticleTest</code> class defines a <em>test case</em> because it inherits from <code>ActiveSupport::TestCase</code>. <code>ArticleTest</code> thus has all the methods available from <code>ActiveSupport::TestCase</code>. You'll see those methods a little later in this guide.</p><p>Any method defined within a class inherited from <code>Minitest::Test</code>
353
- (which is the superclass of <code>ActiveSupport::TestCase</code>) that begins with <code>test_</code> (case sensitive) is simply called a test. So, <code>test_password</code> and <code>test_valid_password</code> are legal test names and are run automatically when the test case is run.</p><p>Rails adds a <code>test</code> method that takes a test name and a block. It generates a normal <code>Minitest::Unit</code> test with method names prefixed with <code>test_</code>. So,</p><div class="code_container">
354
- <pre class="brush: ruby; gutter: false; toolbar: false">
355
- test "the truth" do
356
- assert true
357
- end
358
-
359
- </pre>
360
- </div>
361
- <p>acts as if you had written</p><div class="code_container">
362
- <pre class="brush: ruby; gutter: false; toolbar: false">
363
- def test_the_truth
364
- assert true
365
- end
366
-
367
- </pre>
368
- </div>
369
- <p>only the <code>test</code> macro allows a more readable test name. You can still use regular method definitions though.</p><div class="note"><p>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 <code>define_method</code> and <code>send</code> calls, but formally there's no restriction.</p></div><div class="code_container">
370
- <pre class="brush: ruby; gutter: false; toolbar: false">
371
- assert true
372
-
373
- </pre>
374
- </div>
375
- <p>This line of code is called an <em>assertion</em>. An assertion is a line of code that evaluates an object (or expression) for expected results. For example, an assertion can check:</p>
376
- <ul>
377
- <li>does this value = that value?</li>
378
- <li>is this object nil?</li>
379
- <li>does this line of code throw an exception?</li>
380
- <li>is the user's password greater than 5 characters?</li>
381
- </ul>
382
- <p>Every test contains one or more assertions. Only when all the assertions are successful will the test pass.</p><h4 id="maintaining-the-test-database-schema">3.1 Maintaining the test database schema</h4><p>In order to run your tests, your test database will need to have the current structure. The test helper checks whether your test database has any pending migrations. If so, it will try to load your <code>db/schema.rb</code> or <code>db/structure.sql</code> into the test database. If migrations are still pending, an error will be raised.</p><h4 id="running-tests">3.2 Running Tests</h4><p>Running a test is as simple as invoking the file containing the test cases through <code>rake test</code> command.</p><div class="code_container">
383
- <pre class="brush: plain; gutter: false; toolbar: false">
384
- $ bin/rake test test/models/article_test.rb
385
- .
386
-
387
- Finished tests in 0.009262s, 107.9680 tests/s, 107.9680 assertions/s.
388
-
389
- 1 tests, 1 assertions, 0 failures, 0 errors, 0 skips
390
-
391
- </pre>
392
- </div>
393
- <p>You can also run a particular test method from the test case by running the test and providing the <code>test method name</code>.</p><div class="code_container">
394
- <pre class="brush: plain; gutter: false; toolbar: false">
395
- $ bin/rake test test/models/article_test.rb test_the_truth
396
- .
397
-
398
- Finished tests in 0.009064s, 110.3266 tests/s, 110.3266 assertions/s.
399
-
400
- 1 tests, 1 assertions, 0 failures, 0 errors, 0 skips
401
-
402
- </pre>
403
- </div>
404
- <p>This will run all test methods from the test case. Note that <code>test_helper.rb</code> is in the <code>test</code> directory, hence this directory needs to be added to the load path using the <code>-I</code> switch.</p><p>The <code>.</code> (dot) above indicates a passing test. When a test fails you see an <code>F</code>; when a test throws an error you see an <code>E</code> in its place. The last line of the output is the summary.</p><p>To see how a test failure is reported, you can add a failing test to the <code>article_test.rb</code> test case.</p><div class="code_container">
405
- <pre class="brush: ruby; gutter: false; toolbar: false">
406
- test "should not save article without title" do
407
- article = Article.new
408
- assert_not article.save
409
- end
410
-
411
- </pre>
412
- </div>
413
- <p>Let us run this newly added test.</p><div class="code_container">
414
- <pre class="brush: plain; gutter: false; toolbar: false">
415
- $ bin/rake test test/models/article_test.rb test_should_not_save_article_without_title
416
- F
417
-
418
- Finished tests in 0.044632s, 22.4054 tests/s, 22.4054 assertions/s.
419
-
420
- 1) Failure:
421
- test_should_not_save_article_without_title(ArticleTest) [test/models/article_test.rb:6]:
422
- Failed assertion, no message given.
423
-
424
- 1 tests, 1 assertions, 1 failures, 0 errors, 0 skips
425
-
426
- </pre>
427
- </div>
428
- <p>In the output, <code>F</code> denotes a failure. You can see the corresponding trace shown under <code>1)</code> 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:</p><div class="code_container">
429
- <pre class="brush: ruby; gutter: false; toolbar: false">
430
- test "should not save article without title" do
431
- article = Article.new
432
- assert_not article.save, "Saved the article without a title"
433
- end
434
-
435
- </pre>
436
- </div>
437
- <p>Running this test shows the friendlier assertion message:</p><div class="code_container">
438
- <pre class="brush: plain; gutter: false; toolbar: false">
439
- 1) Failure:
440
- test_should_not_save_article_without_title(ArticleTest) [test/models/article_test.rb:6]:
441
- Saved the article without a title
442
-
443
- </pre>
444
- </div>
445
- <p>Now to get this test to pass we can add a model level validation for the <em>title</em> field.</p><div class="code_container">
446
- <pre class="brush: ruby; gutter: false; toolbar: false">
447
- class Article &lt; ActiveRecord::Base
448
- validates :title, presence: true
449
- end
450
-
451
- </pre>
452
- </div>
453
- <p>Now the test should pass. Let us verify by running the test again:</p><div class="code_container">
454
- <pre class="brush: plain; gutter: false; toolbar: false">
455
- $ bin/rake test test/models/article_test.rb test_should_not_save_article_without_title
456
- .
457
-
458
- Finished tests in 0.047721s, 20.9551 tests/s, 20.9551 assertions/s.
459
-
460
- 1 tests, 1 assertions, 0 failures, 0 errors, 0 skips
461
-
462
- </pre>
463
- </div>
464
- <p>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 <em>Test-Driven Development</em> (TDD).</p><div class="info"><p>Many Rails developers practice <em>Test-Driven Development</em> (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 <a href="http://andrzejonsoftware.blogspot.com/2007/05/15-tdd-steps-to-create-rails.html">15 TDD steps to create a Rails application</a>.</p></div><p>To see how an error gets reported, here's a test containing an error:</p><div class="code_container">
465
- <pre class="brush: ruby; gutter: false; toolbar: false">
466
- test "should report error" do
467
- # some_undefined_variable is not defined elsewhere in the test case
468
- some_undefined_variable
469
- assert true
470
- end
471
-
472
- </pre>
473
- </div>
474
- <p>Now you can see even more output in the console from running the tests:</p><div class="code_container">
475
- <pre class="brush: plain; gutter: false; toolbar: false">
476
- $ bin/rake test test/models/article_test.rb test_should_report_error
477
- E
478
-
479
- Finished tests in 0.030974s, 32.2851 tests/s, 0.0000 assertions/s.
480
-
481
- 1) Error:
482
- test_should_report_error(ArticleTest):
483
- NameError: undefined local variable or method `some_undefined_variable' for #&lt;ArticleTest:0x007fe32e24afe0&gt;
484
- test/models/article_test.rb:10:in `block in &lt;class:ArticleTest&gt;'
485
-
486
- 1 tests, 0 assertions, 0 failures, 1 errors, 0 skips
487
-
488
- </pre>
489
- </div>
490
- <p>Notice the 'E' in the output. It denotes a test with error.</p><div class="note"><p>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.</p></div><p>When a test fails you are presented with the corresponding backtrace. By default
491
- Rails filters that backtrace and will only print lines relevant to your
492
- application. This eliminates the framework noise and helps to focus on your
493
- code. However there are situations when you want to see the full
494
- backtrace. simply set the <code>BACKTRACE</code> environment variable to enable this
495
- behavior:</p><div class="code_container">
496
- <pre class="brush: plain; gutter: false; toolbar: false">
497
- $ BACKTRACE=1 bin/rake test test/models/article_test.rb
498
-
499
- </pre>
500
- </div>
501
- <h4 id="what-to-include-in-your-unit-tests">3.3 What to Include in Your Unit Tests</h4><p>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.</p><h4 id="available-assertions">3.4 Available Assertions</h4><p>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.</p><p>There are a bunch of different types of assertions you can use.
502
- Here's an extract of the assertions you can use with <a href="https://github.com/seattlerb/minitest"><code>Minitest</code></a>, the default testing library used by Rails. The <code>[msg]</code> parameter is an optional string message you can specify to make your test failure messages clearer. It's not required.</p>
503
- <table>
504
- <thead>
505
- <tr>
506
- <th>Assertion</th>
507
- <th>Purpose</th>
508
- </tr>
509
- </thead>
510
- <tbody>
511
- <tr>
512
- <td><code>assert( test, [msg] )</code></td>
513
- <td>Ensures that <code>test</code> is true.</td>
514
- </tr>
515
- <tr>
516
- <td><code>assert_not( test, [msg] )</code></td>
517
- <td>Ensures that <code>test</code> is false.</td>
518
- </tr>
519
- <tr>
520
- <td><code>assert_equal( expected, actual, [msg] )</code></td>
521
- <td>Ensures that <code>expected == actual</code> is true.</td>
522
- </tr>
523
- <tr>
524
- <td><code>assert_not_equal( expected, actual, [msg] )</code></td>
525
- <td>Ensures that <code>expected != actual</code> is true.</td>
526
- </tr>
527
- <tr>
528
- <td><code>assert_same( expected, actual, [msg] )</code></td>
529
- <td>Ensures that <code>expected.equal?(actual)</code> is true.</td>
530
- </tr>
531
- <tr>
532
- <td><code>assert_not_same( expected, actual, [msg] )</code></td>
533
- <td>Ensures that <code>expected.equal?(actual)</code> is false.</td>
534
- </tr>
535
- <tr>
536
- <td><code>assert_nil( obj, [msg] )</code></td>
537
- <td>Ensures that <code>obj.nil?</code> is true.</td>
538
- </tr>
539
- <tr>
540
- <td><code>assert_not_nil( obj, [msg] )</code></td>
541
- <td>Ensures that <code>obj.nil?</code> is false.</td>
542
- </tr>
543
- <tr>
544
- <td><code>assert_empty( obj, [msg] )</code></td>
545
- <td>Ensures that <code>obj</code> is <code>empty?</code>.</td>
546
- </tr>
547
- <tr>
548
- <td><code>assert_not_empty( obj, [msg] )</code></td>
549
- <td>Ensures that <code>obj</code> is not <code>empty?</code>.</td>
550
- </tr>
551
- <tr>
552
- <td><code>assert_match( regexp, string, [msg] )</code></td>
553
- <td>Ensures that a string matches the regular expression.</td>
554
- </tr>
555
- <tr>
556
- <td><code>assert_no_match( regexp, string, [msg] )</code></td>
557
- <td>Ensures that a string doesn't match the regular expression.</td>
558
- </tr>
559
- <tr>
560
- <td><code>assert_includes( collection, obj, [msg] )</code></td>
561
- <td>Ensures that <code>obj</code> is in <code>collection</code>.</td>
562
- </tr>
563
- <tr>
564
- <td><code>assert_not_includes( collection, obj, [msg] )</code></td>
565
- <td>Ensures that <code>obj</code> is not in <code>collection</code>.</td>
566
- </tr>
567
- <tr>
568
- <td><code>assert_in_delta( expecting, actual, [delta], [msg] )</code></td>
569
- <td>Ensures that the numbers <code>expected</code> and <code>actual</code> are within <code>delta</code> of each other.</td>
570
- </tr>
571
- <tr>
572
- <td><code>assert_not_in_delta( expecting, actual, [delta], [msg] )</code></td>
573
- <td>Ensures that the numbers <code>expected</code> and <code>actual</code> are not within <code>delta</code> of each other.</td>
574
- </tr>
575
- <tr>
576
- <td><code>assert_throws( symbol, [msg] ) { block }</code></td>
577
- <td>Ensures that the given block throws the symbol.</td>
578
- </tr>
579
- <tr>
580
- <td><code>assert_raises( exception1, exception2, ... ) { block }</code></td>
581
- <td>Ensures that the given block raises one of the given exceptions.</td>
582
- </tr>
583
- <tr>
584
- <td><code>assert_nothing_raised( exception1, exception2, ... ) { block }</code></td>
585
- <td>Ensures that the given block doesn't raise one of the given exceptions.</td>
586
- </tr>
587
- <tr>
588
- <td><code>assert_instance_of( class, obj, [msg] )</code></td>
589
- <td>Ensures that <code>obj</code> is an instance of <code>class</code>.</td>
590
- </tr>
591
- <tr>
592
- <td><code>assert_not_instance_of( class, obj, [msg] )</code></td>
593
- <td>Ensures that <code>obj</code> is not an instance of <code>class</code>.</td>
594
- </tr>
595
- <tr>
596
- <td><code>assert_kind_of( class, obj, [msg] )</code></td>
597
- <td>Ensures that <code>obj</code> is or descends from <code>class</code>.</td>
598
- </tr>
599
- <tr>
600
- <td><code>assert_not_kind_of( class, obj, [msg] )</code></td>
601
- <td>Ensures that <code>obj</code> is not an instance of <code>class</code> and is not descending from it.</td>
602
- </tr>
603
- <tr>
604
- <td><code>assert_respond_to( obj, symbol, [msg] )</code></td>
605
- <td>Ensures that <code>obj</code> responds to <code>symbol</code>.</td>
606
- </tr>
607
- <tr>
608
- <td><code>assert_not_respond_to( obj, symbol, [msg] )</code></td>
609
- <td>Ensures that <code>obj</code> does not respond to <code>symbol</code>.</td>
610
- </tr>
611
- <tr>
612
- <td><code>assert_operator( obj1, operator, [obj2], [msg] )</code></td>
613
- <td>Ensures that <code>obj1.operator(obj2)</code> is true.</td>
614
- </tr>
615
- <tr>
616
- <td><code>assert_not_operator( obj1, operator, [obj2], [msg] )</code></td>
617
- <td>Ensures that <code>obj1.operator(obj2)</code> is false.</td>
618
- </tr>
619
- <tr>
620
- <td><code>assert_predicate ( obj, predicate, [msg] )</code></td>
621
- <td>Ensures that <code>obj.predicate</code> is true, e.g. <code>assert_predicate str, :empty?</code>
622
- </td>
623
- </tr>
624
- <tr>
625
- <td><code>assert_not_predicate ( obj, predicate, [msg] )</code></td>
626
- <td>Ensures that <code>obj.predicate</code> is false, e.g. <code>assert_not_predicate str, :empty?</code>
627
- </td>
628
- </tr>
629
- <tr>
630
- <td><code>assert_send( array, [msg] )</code></td>
631
- <td>Ensures that executing the method listed in <code>array[1]</code> on the object in <code>array[0]</code> with the parameters of <code>array[2 and up]</code> is true. This one is weird eh?</td>
632
- </tr>
633
- <tr>
634
- <td><code>flunk( [msg] )</code></td>
635
- <td>Ensures failure. This is useful to explicitly mark a test that isn't finished yet.</td>
636
- </tr>
637
- </tbody>
638
- </table>
639
- <p>The above are subset of assertions that minitest supports. For an exhaustive &amp; more up-to-date list, please check <a href="http://docs.seattlerb.org/minitest/">Minitest API documentation</a>, specifically <a href="http://docs.seattlerb.org/minitest/Minitest/Assertions.html"><code>Minitest::Assertions</code></a></p><p>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.</p><div class="note"><p>Creating your own assertions is an advanced topic that we won't cover in this tutorial.</p></div><h4 id="rails-specific-assertions">3.5 Rails Specific Assertions</h4><p>Rails adds some custom assertions of its own to the <code>minitest</code> framework:</p>
640
- <table>
641
- <thead>
642
- <tr>
643
- <th>Assertion</th>
644
- <th>Purpose</th>
645
- </tr>
646
- </thead>
647
- <tbody>
648
- <tr>
649
- <td><code>assert_difference(expressions, difference = 1, message = nil) {...}</code></td>
650
- <td>Test numeric difference between the return value of an expression as a result of what is evaluated in the yielded block.</td>
651
- </tr>
652
- <tr>
653
- <td><code>assert_no_difference(expressions, message = nil, &amp;amp;block)</code></td>
654
- <td>Asserts that the numeric result of evaluating an expression is not changed before and after invoking the passed in block.</td>
655
- </tr>
656
- <tr>
657
- <td><code>assert_recognizes(expected_options, path, extras={}, message=nil)</code></td>
658
- <td>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.</td>
659
- </tr>
660
- <tr>
661
- <td><code>assert_generates(expected_path, options, defaults={}, extras = {}, message=nil)</code></td>
662
- <td>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.</td>
663
- </tr>
664
- <tr>
665
- <td><code>assert_response(type, message = nil)</code></td>
666
- <td>Asserts that the response comes with a specific status code. You can specify <code>:success</code> to indicate 200-299, <code>:redirect</code> to indicate 300-399, <code>:missing</code> to indicate 404, or <code>:error</code> to match the 500-599 range. You can also pass an explicit status number or its symbolic equivalent. For more information, see <a href="http://rubydoc.info/github/rack/rack/master/Rack/Utils#HTTP_STATUS_CODES-constant">full list of status codes</a> and how their <a href="http://rubydoc.info/github/rack/rack/master/Rack/Utils#SYMBOL_TO_STATUS_CODE-constant">mapping</a> works.</td>
667
- </tr>
668
- <tr>
669
- <td><code>assert_redirected_to(options = {}, message=nil)</code></td>
670
- <td>Assert that the redirection options passed in match those of the redirect called in the latest action. This match can be partial, such that <code>assert_redirected_to(controller: "weblog")</code> will also match the redirection of <code>redirect_to(controller: "weblog", action: "show")</code> and so on. You can also pass named routes such as <code>assert_redirected_to root_path</code> and Active Record objects such as <code>assert_redirected_to @article</code>.</td>
671
- </tr>
672
- <tr>
673
- <td><code>assert_template(expected = nil, message=nil)</code></td>
674
- <td>Asserts that the request was rendered with the appropriate template file.</td>
675
- </tr>
676
- </tbody>
677
- </table>
678
- <p>You'll see the usage of some of these assertions in the next chapter.</p><h3 id="functional-tests-for-your-controllers">4 Functional Tests for Your Controllers</h3><p>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.</p><h4 id="what-to-include-in-your-functional-tests">4.1 What to Include in your Functional Tests</h4><p>You should test for things such as:</p>
679
- <ul>
680
- <li>was the web request successful?</li>
681
- <li>was the user redirected to the right page?</li>
682
- <li>was the user successfully authenticated?</li>
683
- <li>was the correct object stored in the response template?</li>
684
- <li>was the appropriate message displayed to the user in the view?</li>
685
- </ul>
686
- <p>Now that we have used Rails scaffold generator for our <code>Article</code> resource, it has already created the controller code and tests. You can take look at the file <code>articles_controller_test.rb</code> in the <code>test/controllers</code> directory.</p><p>Let me take you through one such test, <code>test_should_get_index</code> from the file <code>articles_controller_test.rb</code>.</p><div class="code_container">
687
- <pre class="brush: ruby; gutter: false; toolbar: false">
688
- class ArticlesControllerTest &lt; ActionController::TestCase
689
- test "should get index" do
690
- get :index
691
- assert_response :success
692
- assert_not_nil assigns(:articles)
693
- end
694
- end
695
-
696
- </pre>
697
- </div>
698
- <p>In the <code>test_should_get_index</code> test, Rails simulates a request on the action called <code>index</code>, making sure the request was successful and also ensuring that it assigns a valid <code>articles</code> instance variable.</p><p>The <code>get</code> method kicks off the web request and populates the results into the response. It accepts 4 arguments:</p>
699
- <ul>
700
- <li>The action of the controller you are requesting. This can be in the form of a string or a symbol.</li>
701
- <li>An optional hash of request parameters to pass into the action (eg. query string parameters or article variables).</li>
702
- <li>An optional hash of session variables to pass along with the request.</li>
703
- <li>An optional hash of flash values.</li>
704
- </ul>
705
- <p>Example: Calling the <code>:show</code> action, passing an <code>id</code> of 12 as the <code>params</code> and setting a <code>user_id</code> of 5 in the session:</p><div class="code_container">
706
- <pre class="brush: ruby; gutter: false; toolbar: false">
707
- get(:show, {'id' =&gt; "12"}, {'user_id' =&gt; 5})
708
-
709
- </pre>
710
- </div>
711
- <p>Another example: Calling the <code>:view</code> action, passing an <code>id</code> of 12 as the <code>params</code>, this time with no session, but with a flash message.</p><div class="code_container">
712
- <pre class="brush: ruby; gutter: false; toolbar: false">
713
- get(:view, {'id' =&gt; '12'}, nil, {'message' =&gt; 'booya!'})
714
-
715
- </pre>
716
- </div>
717
- <div class="note"><p>If you try running <code>test_should_create_article</code> test from <code>articles_controller_test.rb</code> it will fail on account of the newly added model level validation and rightly so.</p></div><p>Let us modify <code>test_should_create_article</code> test in <code>articles_controller_test.rb</code> so that all our test pass:</p><div class="code_container">
718
- <pre class="brush: ruby; gutter: false; toolbar: false">
719
- test "should create article" do
720
- assert_difference('Article.count') do
721
- post :create, article: {title: 'Some title'}
722
- end
723
-
724
- assert_redirected_to article_path(assigns(:article))
725
- end
726
-
727
- </pre>
728
- </div>
729
- <p>Now you can try running all the tests and they should pass.</p><h4 id="available-request-types-for-functional-tests">4.2 Available Request Types for Functional Tests</h4><p>If you're familiar with the HTTP protocol, you'll know that <code>get</code> is a type of request. There are 6 request types supported in Rails functional tests:</p>
730
- <ul>
731
- <li><code>get</code></li>
732
- <li><code>post</code></li>
733
- <li><code>patch</code></li>
734
- <li><code>put</code></li>
735
- <li><code>head</code></li>
736
- <li><code>delete</code></li>
737
- </ul>
738
- <p>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.</p><div class="note"><p>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.</p></div><h4 id="the-four-hashes-of-the-apocalypse">4.3 The Four Hashes of the Apocalypse</h4><p>After a request has been made using one of the 6 methods (<code>get</code>, <code>post</code>, etc.) and processed, you will have 4 Hash objects ready for use:</p>
739
- <ul>
740
- <li>
741
- <code>assigns</code> - Any objects that are stored as instance variables in actions for use in views.</li>
742
- <li>
743
- <code>cookies</code> - Any cookies that are set.</li>
744
- <li>
745
- <code>flash</code> - Any objects living in the flash.</li>
746
- <li>
747
- <code>session</code> - Any object living in session variables.</li>
748
- </ul>
749
- <p>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 <code>assigns</code>. For example:</p><div class="code_container">
750
- <pre class="brush: ruby; gutter: false; toolbar: false">
751
- flash["gordon"] flash[:gordon]
752
- session["shmession"] session[:shmession]
753
- cookies["are_good_for_u"] cookies[:are_good_for_u]
754
-
755
- # Because you can't use assigns[:something] for historical reasons:
756
- assigns["something"] assigns(:something)
757
-
758
- </pre>
759
- </div>
760
- <h4 id="instance-variables-available">4.4 Instance Variables Available</h4><p>You also have access to three instance variables in your functional tests:</p>
761
- <ul>
762
- <li>
763
- <code>@controller</code> - The controller processing the request</li>
764
- <li>
765
- <code>@request</code> - The request</li>
766
- <li>
767
- <code>@response</code> - The response</li>
768
- </ul>
769
- <h4 id="setting-headers-and-cgi-variables">4.5 Setting Headers and CGI variables</h4><p><a href="http://tools.ietf.org/search/rfc2616#section-5.3">HTTP headers</a>
770
- and
771
- <a href="http://tools.ietf.org/search/rfc3875#section-4.1">CGI variables</a>
772
- can be set directly on the <code>@request</code> instance variable:</p><div class="code_container">
773
- <pre class="brush: ruby; gutter: false; toolbar: false">
774
- # setting a HTTP Header
775
- @request.headers["Accept"] = "text/plain, text/html"
776
- get :index # simulate the request with custom header
777
-
778
- # setting a CGI variable
779
- @request.headers["HTTP_REFERER"] = "http://example.com/home"
780
- post :create # simulate the request with custom env variable
781
-
782
- </pre>
783
- </div>
784
- <h4 id="testing-templates-and-layouts">4.6 Testing Templates and Layouts</h4><p>If you want to make sure that the response rendered the correct template and layout, you can use the <code>assert_template</code>
785
- method:</p><div class="code_container">
786
- <pre class="brush: ruby; gutter: false; toolbar: false">
787
- test "index should render correct template and layout" do
788
- get :index
789
- assert_template :index
790
- assert_template layout: "layouts/application"
791
- end
792
-
793
- </pre>
794
- </div>
795
- <p>Note that you cannot test for template and layout at the same time, with one call to <code>assert_template</code> method.
796
- Also, for the <code>layout</code> test, you can give a regular expression instead of a string, but using the string, makes
797
- things clearer. On the other hand, you have to include the "layouts" directory name even if you save your layout
798
- file in this standard layout directory. Hence,</p><div class="code_container">
799
- <pre class="brush: ruby; gutter: false; toolbar: false">
800
- assert_template layout: "application"
801
-
802
- </pre>
803
- </div>
804
- <p>will not work.</p><p>If your view renders any partial, when asserting for the layout, you have to assert for the partial at the same time.
805
- Otherwise, assertion will fail.</p><p>Hence:</p><div class="code_container">
806
- <pre class="brush: ruby; gutter: false; toolbar: false">
807
- test "new should render correct layout" do
808
- get :new
809
- assert_template layout: "layouts/application", partial: "_form"
810
- end
811
-
812
- </pre>
813
- </div>
814
- <p>is the correct way to assert for the layout when the view renders a partial with name <code>_form</code>. Omitting the <code>:partial</code> key in your <code>assert_template</code> call will complain.</p><h4 id="a-fuller-functional-test-example">4.7 A Fuller Functional Test Example</h4><p>Here's another example that uses <code>flash</code>, <code>assert_redirected_to</code>, and <code>assert_difference</code>:</p><div class="code_container">
815
- <pre class="brush: ruby; gutter: false; toolbar: false">
816
- test "should create article" do
817
- assert_difference('Article.count') do
818
- post :create, article: {title: 'Hi', body: 'This is my first article.'}
819
- end
820
- assert_redirected_to article_path(assigns(:article))
821
- assert_equal 'Article was successfully created.', flash[:notice]
822
- end
823
-
824
- </pre>
825
- </div>
826
- <h4 id="testing-views">4.8 Testing Views</h4><p>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 <code>assert_select</code> assertion allows you to do this by using a simple yet powerful syntax.</p><div class="note"><p>You may find references to <code>assert_tag</code> in other documentation. This has been removed in 4.2. Use <code>assert_select</code> instead.</p></div><p>There are two forms of <code>assert_select</code>:</p><p><code>assert_select(selector, [equality], [message])</code> ensures that the equality condition is met on the selected elements through the selector. The selector may be a CSS selector expression (String) or an expression with substitution values.</p><p><code>assert_select(element, selector, [equality], [message])</code> ensures that the equality condition is met on all the selected elements through the selector starting from the <em>element</em> (instance of <code>Nokogiri::XML::Node</code> or <code>Nokogiri::XML::NodeSet</code>) and its descendants.</p><p>For example, you could verify the contents on the title element in your response with:</p><div class="code_container">
827
- <pre class="brush: ruby; gutter: false; toolbar: false">
828
- assert_select 'title', "Welcome to Rails Testing Guide"
829
-
830
- </pre>
831
- </div>
832
- <p>You can also use nested <code>assert_select</code> blocks. In this case the inner <code>assert_select</code> runs the assertion on the complete collection of elements selected by the outer <code>assert_select</code> block:</p><div class="code_container">
833
- <pre class="brush: ruby; gutter: false; toolbar: false">
834
- assert_select 'ul.navigation' do
835
- assert_select 'li.menu_item'
836
- end
837
-
838
- </pre>
839
- </div>
840
- <p>Alternatively the collection of elements selected by the outer <code>assert_select</code> may be iterated through so that <code>assert_select</code> 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.</p><div class="code_container">
841
- <pre class="brush: ruby; gutter: false; toolbar: false">
842
- assert_select "ol" do |elements|
843
- elements.each do |element|
844
- assert_select element, "li", 4
845
- end
846
- end
847
-
848
- assert_select "ol" do
849
- assert_select "li", 8
850
- end
851
-
852
- </pre>
853
- </div>
854
- <p>The <code>assert_select</code> assertion is quite powerful. For more advanced usage, refer to its <a href="https://github.com/rails/rails-dom-testing/blob/master/lib/rails/dom/testing/assertions/selector_assertions.rb">documentation</a>.</p><h5 id="additional-view-based-assertions">4.8.1 Additional View-Based Assertions</h5><p>There are more assertions that are primarily used in testing views:</p>
855
- <table>
856
- <thead>
857
- <tr>
858
- <th>Assertion</th>
859
- <th>Purpose</th>
860
- </tr>
861
- </thead>
862
- <tbody>
863
- <tr>
864
- <td><code>assert_select_email</code></td>
865
- <td>Allows you to make assertions on the body of an e-mail.</td>
866
- </tr>
867
- <tr>
868
- <td><code>assert_select_encoded</code></td>
869
- <td>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.</td>
870
- </tr>
871
- <tr>
872
- <td>
873
- <code>css_select(selector)</code> or <code>css_select(element, selector)</code>
874
- </td>
875
- <td>Returns an array of all the elements selected by the <em>selector</em>. In the second variant it first matches the base <em>element</em> and tries to match the <em>selector</em> expression on any of its children. If there are no matches both variants return an empty array.</td>
876
- </tr>
877
- </tbody>
878
- </table>
879
- <p>Here's an example of using <code>assert_select_email</code>:</p><div class="code_container">
880
- <pre class="brush: ruby; gutter: false; toolbar: false">
881
- assert_select_email do
882
- assert_select 'small', 'Please click the "Unsubscribe" link if you want to opt-out.'
883
- end
884
-
885
- </pre>
886
- </div>
887
- <h3 id="integration-testing">5 Integration Testing</h3><p>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.</p><p>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.</p><div class="code_container">
888
- <pre class="brush: plain; gutter: false; toolbar: false">
889
- $ bin/rails generate integration_test user_flows
890
- exists test/integration/
891
- create test/integration/user_flows_test.rb
892
-
893
- </pre>
894
- </div>
895
- <p>Here's what a freshly-generated integration test looks like:</p><div class="code_container">
896
- <pre class="brush: ruby; gutter: false; toolbar: false">
897
- require 'test_helper'
898
-
899
- class UserFlowsTest &lt; ActionDispatch::IntegrationTest
900
- # test "the truth" do
901
- # assert true
902
- # end
903
- end
904
-
905
- </pre>
906
- </div>
907
- <p>Integration tests inherit from <code>ActionDispatch::IntegrationTest</code>. 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.</p><h4 id="helpers-available-for-integration-tests">5.1 Helpers Available for Integration Tests</h4><p>In addition to the standard testing helpers, there are some additional helpers available to integration tests:</p>
908
- <table>
909
- <thead>
910
- <tr>
911
- <th>Helper</th>
912
- <th>Purpose</th>
913
- </tr>
914
- </thead>
915
- <tbody>
916
- <tr>
917
- <td><code>https?</code></td>
918
- <td>Returns <code>true</code> if the session is mimicking a secure HTTPS request.</td>
919
- </tr>
920
- <tr>
921
- <td><code>https!</code></td>
922
- <td>Allows you to mimic a secure HTTPS request.</td>
923
- </tr>
924
- <tr>
925
- <td><code>host!</code></td>
926
- <td>Allows you to set the host name to use in the next request.</td>
927
- </tr>
928
- <tr>
929
- <td><code>redirect?</code></td>
930
- <td>Returns <code>true</code> if the last request was a redirect.</td>
931
- </tr>
932
- <tr>
933
- <td><code>follow_redirect!</code></td>
934
- <td>Follows a single redirect response.</td>
935
- </tr>
936
- <tr>
937
- <td><code>request_via_redirect(http_method, path, [parameters], [headers])</code></td>
938
- <td>Allows you to make an HTTP request and follow any subsequent redirects.</td>
939
- </tr>
940
- <tr>
941
- <td><code>post_via_redirect(path, [parameters], [headers])</code></td>
942
- <td>Allows you to make an HTTP POST request and follow any subsequent redirects.</td>
943
- </tr>
944
- <tr>
945
- <td><code>get_via_redirect(path, [parameters], [headers])</code></td>
946
- <td>Allows you to make an HTTP GET request and follow any subsequent redirects.</td>
947
- </tr>
948
- <tr>
949
- <td><code>patch_via_redirect(path, [parameters], [headers])</code></td>
950
- <td>Allows you to make an HTTP PATCH request and follow any subsequent redirects.</td>
951
- </tr>
952
- <tr>
953
- <td><code>put_via_redirect(path, [parameters], [headers])</code></td>
954
- <td>Allows you to make an HTTP PUT request and follow any subsequent redirects.</td>
955
- </tr>
956
- <tr>
957
- <td><code>delete_via_redirect(path, [parameters], [headers])</code></td>
958
- <td>Allows you to make an HTTP DELETE request and follow any subsequent redirects.</td>
959
- </tr>
960
- <tr>
961
- <td><code>open_session</code></td>
962
- <td>Opens a new session instance.</td>
963
- </tr>
964
- </tbody>
965
- </table>
966
- <h4 id="integration-testing-examples">5.2 Integration Testing Examples</h4><p>A simple integration test that exercises multiple controllers:</p><div class="code_container">
967
- <pre class="brush: ruby; gutter: false; toolbar: false">
968
- require 'test_helper'
969
-
970
- class UserFlowsTest &lt; ActionDispatch::IntegrationTest
971
- test "login and browse site" do
972
- # login via https
973
- https!
974
- get "/login"
975
- assert_response :success
976
-
977
- post_via_redirect "/login", username: users(:david).username, password: users(:david).password
978
- assert_equal '/welcome', path
979
- assert_equal 'Welcome david!', flash[:notice]
980
-
981
- https!(false)
982
- get "/articles/all"
983
- assert_response :success
984
- assert assigns(:products)
985
- end
986
- end
987
-
988
- </pre>
989
- </div>
990
- <p>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.</p><p>Here's an example of multiple sessions and custom DSL in an integration test</p><div class="code_container">
991
- <pre class="brush: ruby; gutter: false; toolbar: false">
992
- require 'test_helper'
993
-
994
- class UserFlowsTest &lt; ActionDispatch::IntegrationTest
995
- test "login and browse site" do
996
- # User david logs in
997
- david = login(:david)
998
- # User guest logs in
999
- guest = login(:guest)
1000
-
1001
- # Both are now available in different sessions
1002
- assert_equal 'Welcome david!', david.flash[:notice]
1003
- assert_equal 'Welcome guest!', guest.flash[:notice]
1004
-
1005
- # User david can browse site
1006
- david.browses_site
1007
- # User guest can browse site as well
1008
- guest.browses_site
1009
-
1010
- # Continue with other assertions
1011
- end
1012
-
1013
- private
1014
-
1015
- module CustomDsl
1016
- def browses_site
1017
- get "/products/all"
1018
- assert_response :success
1019
- assert assigns(:products)
1020
- end
1021
- end
1022
-
1023
- def login(user)
1024
- open_session do |sess|
1025
- sess.extend(CustomDsl)
1026
- u = users(user)
1027
- sess.https!
1028
- sess.post "/login", username: u.username, password: u.password
1029
- assert_equal '/welcome', sess.path
1030
- sess.https!(false)
1031
- end
1032
- end
1033
- end
1034
-
1035
- </pre>
1036
- </div>
1037
- <h3 id="rake-tasks-for-running-your-tests">6 Rake Tasks for Running your Tests</h3><p>You don't need to set up and run your tests by hand on a test-by-test basis.
1038
- Rails comes with a number of commands to help in testing.
1039
- The table below lists all commands that come along in the default Rakefile
1040
- when you initiate a Rails project.</p>
1041
- <table>
1042
- <thead>
1043
- <tr>
1044
- <th>Tasks</th>
1045
- <th>Description</th>
1046
- </tr>
1047
- </thead>
1048
- <tbody>
1049
- <tr>
1050
- <td><code>rake test</code></td>
1051
- <td>Runs all unit, functional and integration tests. You can also simply run <code>rake</code> as Rails will run all the tests by default</td>
1052
- </tr>
1053
- <tr>
1054
- <td><code>rake test:controllers</code></td>
1055
- <td>Runs all the controller tests from <code>test/controllers</code>
1056
- </td>
1057
- </tr>
1058
- <tr>
1059
- <td><code>rake test:functionals</code></td>
1060
- <td>Runs all the functional tests from <code>test/controllers</code>, <code>test/mailers</code>, and <code>test/functional</code>
1061
- </td>
1062
- </tr>
1063
- <tr>
1064
- <td><code>rake test:helpers</code></td>
1065
- <td>Runs all the helper tests from <code>test/helpers</code>
1066
- </td>
1067
- </tr>
1068
- <tr>
1069
- <td><code>rake test:integration</code></td>
1070
- <td>Runs all the integration tests from <code>test/integration</code>
1071
- </td>
1072
- </tr>
1073
- <tr>
1074
- <td><code>rake test:mailers</code></td>
1075
- <td>Runs all the mailer tests from <code>test/mailers</code>
1076
- </td>
1077
- </tr>
1078
- <tr>
1079
- <td><code>rake test:models</code></td>
1080
- <td>Runs all the model tests from <code>test/models</code>
1081
- </td>
1082
- </tr>
1083
- <tr>
1084
- <td><code>rake test:units</code></td>
1085
- <td>Runs all the unit tests from <code>test/models</code>, <code>test/helpers</code>, and <code>test/unit</code>
1086
- </td>
1087
- </tr>
1088
- <tr>
1089
- <td><code>rake test:all</code></td>
1090
- <td>Runs all tests quickly by merging all types and not resetting db</td>
1091
- </tr>
1092
- <tr>
1093
- <td><code>rake test:all:db</code></td>
1094
- <td>Runs all tests quickly by merging all types and resetting db</td>
1095
- </tr>
1096
- </tbody>
1097
- </table>
1098
- <h3 id="brief-note-about-minitest">7 Brief Note About <code>Minitest</code>
1099
- </h3><p>Ruby ships with a vast Standard Library for all common use-cases including testing. Since version 1.9, Ruby provides <code>Minitest</code>, a framework for testing. All the basic assertions such as <code>assert_equal</code> discussed above are actually defined in <code>Minitest::Assertions</code>. The classes <code>ActiveSupport::TestCase</code>, <code>ActionController::TestCase</code>, <code>ActionMailer::TestCase</code>, <code>ActionView::TestCase</code> and <code>ActionDispatch::IntegrationTest</code> - which we have been inheriting in our test classes - include <code>Minitest::Assertions</code>, allowing us to use all of the basic assertions in our tests.</p><div class="note"><p>For more information on <code>Minitest</code>, refer to <a href="http://ruby-doc.org/stdlib-2.1.0/libdoc/minitest/rdoc/MiniTest.html">Minitest</a></p></div><h3 id="setup-and-teardown">8 Setup and Teardown</h3><p>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 <code>Articles</code> controller:</p><div class="code_container">
1100
- <pre class="brush: ruby; gutter: false; toolbar: false">
1101
- require 'test_helper'
1102
-
1103
- class ArticlesControllerTest &lt; ActionController::TestCase
1104
-
1105
- # called before every single test
1106
- def setup
1107
- @article = articles(:one)
1108
- end
1109
-
1110
- # called after every single test
1111
- def teardown
1112
- # as we are re-initializing @article before every test
1113
- # setting it to nil here is not essential but I hope
1114
- # you understand how you can use the teardown method
1115
- @article = nil
1116
- end
1117
-
1118
- test "should show article" do
1119
- get :show, id: @article.id
1120
- assert_response :success
1121
- end
1122
-
1123
- test "should destroy article" do
1124
- assert_difference('Article.count', -1) do
1125
- delete :destroy, id: @article.id
1126
- end
1127
-
1128
- assert_redirected_to articles_path
1129
- end
1130
-
1131
- end
1132
-
1133
- </pre>
1134
- </div>
1135
- <p>Above, the <code>setup</code> method is called before each test and so <code>@article</code> is available for each of the tests. Rails implements <code>setup</code> and <code>teardown</code> as <code>ActiveSupport::Callbacks</code>. Which essentially means you need not only use <code>setup</code> and <code>teardown</code> as methods in your tests. You could specify them by using:</p>
1136
- <ul>
1137
- <li>a block</li>
1138
- <li>a method (like in the earlier example)</li>
1139
- <li>a method name as a symbol</li>
1140
- <li>a lambda</li>
1141
- </ul>
1142
- <p>Let's see the earlier example by specifying <code>setup</code> callback by specifying a method name as a symbol:</p><div class="code_container">
1143
- <pre class="brush: ruby; gutter: false; toolbar: false">
1144
- require 'test_helper'
1145
-
1146
- class ArticlesControllerTest &lt; ActionController::TestCase
1147
-
1148
- # called before every single test
1149
- setup :initialize_article
1150
-
1151
- # called after every single test
1152
- def teardown
1153
- @article = nil
1154
- end
1155
-
1156
- test "should show article" do
1157
- get :show, id: @article.id
1158
- assert_response :success
1159
- end
1160
-
1161
- test "should update article" do
1162
- patch :update, id: @article.id, article: {}
1163
- assert_redirected_to article_path(assigns(:article))
1164
- end
1165
-
1166
- test "should destroy article" do
1167
- assert_difference('Article.count', -1) do
1168
- delete :destroy, id: @article.id
1169
- end
1170
-
1171
- assert_redirected_to articles_path
1172
- end
1173
-
1174
- private
1175
-
1176
- def initialize_article
1177
- @article = articles(:one)
1178
- end
1179
- end
1180
-
1181
- </pre>
1182
- </div>
1183
- <h3 id="testing-routes">9 Testing Routes</h3><p>Like everything else in your Rails application, it is recommended that you test your routes. An example test for a route in the default <code>show</code> action of <code>Articles</code> controller above should look like:</p><div class="code_container">
1184
- <pre class="brush: ruby; gutter: false; toolbar: false">
1185
- test "should route to article" do
1186
- assert_routing '/articles/1', {controller: "articles", action: "show", id: "1"}
1187
- end
1188
-
1189
- </pre>
1190
- </div>
1191
- <h3 id="testing-your-mailers">10 Testing Your Mailers</h3><p>Testing mailer classes requires some specific tools to do a thorough job.</p><h4 id="keeping-the-postman-in-check">10.1 Keeping the Postman in Check</h4><p>Your mailer classes - like every other part of your Rails application - should be tested to ensure that it is working as expected.</p><p>The goals of testing your mailer classes are to ensure that:</p>
1192
- <ul>
1193
- <li>emails are being processed (created and sent)</li>
1194
- <li>the email content is correct (subject, sender, body, etc)</li>
1195
- <li>the right emails are being sent at the right times</li>
1196
- </ul>
1197
- <h5 id="from-all-sides">10.1.1 From All Sides</h5><p>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.</p><h4 id="unit-testing">10.2 Unit Testing</h4><p>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.</p><h5 id="revenge-of-the-fixtures">10.2.1 Revenge of the Fixtures</h5><p>For the purposes of unit testing a mailer, fixtures are used to provide an example of how the output <em>should</em> 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 <code>test/fixtures</code> directly corresponds to the name of the mailer. So, for a mailer named <code>UserMailer</code>, the fixtures should reside in <code>test/fixtures/user_mailer</code> directory.</p><p>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.</p><h5 id="the-basic-test-case">10.2.2 The Basic Test Case</h5><p>Here's a unit test to test a mailer named <code>UserMailer</code> whose action <code>invite</code> is used to send an invitation to a friend. It is an adapted version of the base test created by the generator for an <code>invite</code> action.</p><div class="code_container">
1198
- <pre class="brush: ruby; gutter: false; toolbar: false">
1199
- require 'test_helper'
1200
-
1201
- class UserMailerTest &lt; ActionMailer::TestCase
1202
- test "invite" do
1203
- # Send the email, then test that it got queued
1204
- email = UserMailer.create_invite('me@example.com',
1205
- 'friend@example.com', Time.now).deliver
1206
- assert_not ActionMailer::Base.deliveries.empty?
1207
-
1208
- # Test the body of the sent email contains what we expect it to
1209
- assert_equal ['me@example.com'], email.from
1210
- assert_equal ['friend@example.com'], email.to
1211
- assert_equal 'You have been invited by me@example.com', email.subject
1212
- assert_equal read_fixture('invite').join, email.body.to_s
1213
- end
1214
- end
1215
-
1216
- </pre>
1217
- </div>
1218
- <p>In the test we send the email and store the returned object in the <code>email</code>
1219
- variable. We then ensure that it was sent (the first assert), then, in the
1220
- second batch of assertions, we ensure that the email does indeed contain what we
1221
- expect. The helper <code>read_fixture</code> is used to read in the content from this file.</p><p>Here's the content of the <code>invite</code> fixture:</p><div class="code_container">
1222
- <pre class="brush: plain; gutter: false; toolbar: false">
1223
- Hi friend@example.com,
1224
-
1225
- You have been invited.
1226
-
1227
- Cheers!
1228
-
1229
- </pre>
1230
- </div>
1231
- <p>This is the right time to understand a little more about writing tests for your
1232
- mailers. The line <code>ActionMailer::Base.delivery_method = :test</code> in
1233
- <code>config/environments/test.rb</code> sets the delivery method to test mode so that
1234
- email will not actually be delivered (useful to avoid spamming your users while
1235
- testing) but instead it will be appended to an array
1236
- (<code>ActionMailer::Base.deliveries</code>).</p><div class="note"><p>The <code>ActionMailer::Base.deliveries</code> array is only reset automatically in
1237
- <code>ActionMailer::TestCase</code> tests. If you want to have a clean slate outside Action
1238
- Mailer tests, you can reset it manually with:
1239
- <code>ActionMailer::Base.deliveries.clear</code></p></div><h4 id="functional-testing">10.3 Functional Testing</h4><p>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:</p><div class="code_container">
1240
- <pre class="brush: ruby; gutter: false; toolbar: false">
1241
- require 'test_helper'
1242
-
1243
- class UserControllerTest &lt; ActionController::TestCase
1244
- test "invite friend" do
1245
- assert_difference 'ActionMailer::Base.deliveries.size', +1 do
1246
- post :invite_friend, email: 'friend@example.com'
1247
- end
1248
- invite_email = ActionMailer::Base.deliveries.last
1249
-
1250
- assert_equal "You have been invited by me@example.com", invite_email.subject
1251
- assert_equal 'friend@example.com', invite_email.to[0]
1252
- assert_match(/Hi friend@example.com/, invite_email.body.to_s)
1253
- end
1254
- end
1255
-
1256
- </pre>
1257
- </div>
1258
- <h3 id="testing-helpers">11 Testing helpers</h3><p>In order to test helpers, all you need to do is check that the output of the
1259
- helper method matches what you'd expect. Tests related to the helpers are
1260
- located under the <code>test/helpers</code> directory.</p><p>A helper test looks like so:</p><div class="code_container">
1261
- <pre class="brush: ruby; gutter: false; toolbar: false">
1262
- require 'test_helper'
1263
-
1264
- class UserHelperTest &lt; ActionView::TestCase
1265
- end
1266
-
1267
- </pre>
1268
- </div>
1269
- <p>A helper is just a simple module where you can define methods which are
1270
- available into your views. To test the output of the helper's methods, you just
1271
- have to use a mixin like this:</p><div class="code_container">
1272
- <pre class="brush: ruby; gutter: false; toolbar: false">
1273
- class UserHelperTest &lt; ActionView::TestCase
1274
- include UserHelper
1275
-
1276
- test "should return the user name" do
1277
- # ...
1278
- end
1279
- end
1280
-
1281
- </pre>
1282
- </div>
1283
- <p>Moreover, since the test class extends from <code>ActionView::TestCase</code>, you have
1284
- access to Rails' helper methods such as <code>link_to</code> or <code>pluralize</code>.</p><h3 id="other-testing-approaches">12 Other Testing Approaches</h3><p>The built-in <code>minitest</code> 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:</p>
1285
- <ul>
1286
- <li>
1287
- <a href="http://avdi.org/projects/nulldb/">NullDB</a>, a way to speed up testing by avoiding database use.</li>
1288
- <li>
1289
- <a href="https://github.com/thoughtbot/factory_girl/tree/master">Factory Girl</a>, a replacement for fixtures.</li>
1290
- <li>
1291
- <a href="https://github.com/rdy/fixture_builder">Fixture Builder</a>, a tool that compiles Ruby factories into fixtures before a test run.</li>
1292
- <li>
1293
- <a href="https://github.com/metaskills/minitest-spec-rails">MiniTest::Spec Rails</a>, use the MiniTest::Spec DSL within your rails tests.</li>
1294
- <li>
1295
- <a href="http://www.thoughtbot.com/projects/shoulda">Shoulda</a>, an extension to <code>test/unit</code> with additional helpers, macros, and assertions.</li>
1296
- <li>
1297
- <a href="http://relishapp.com/rspec">RSpec</a>, a behavior-driven development framework</li>
1298
- </ul>
1299
-
1300
-
1301
- <h3>Feedback</h3>
1302
- <p>
1303
- You're encouraged to help improve the quality of this guide.
1304
- </p>
1305
- <p>
1306
- Please contribute if you see any typos or factual errors.
1307
- To get started, you can read our <a href="http://edgeguides.rubyonrails.org/contributing_to_ruby_on_rails.html#contributing-to-the-rails-documentation">documentation contributions</a> section.
1308
- </p>
1309
- <p>
1310
- You may also find incomplete content, or stuff that is not up to date.
1311
- Please do add any missing documentation for master. Make sure to check
1312
- <a href="http://edgeguides.rubyonrails.org">Edge Guides</a> first to verify
1313
- if the issues are already fixed or not on the master branch.
1314
- Check the <a href="ruby_on_rails_guides_guidelines.html">Ruby on Rails Guides Guidelines</a>
1315
- for style and conventions.
1316
- </p>
1317
- <p>
1318
- If for whatever reason you spot something to fix but cannot patch it yourself, please
1319
- <a href="https://github.com/rails/rails/issues">open an issue</a>.
1320
- </p>
1321
- <p>And last but not least, any kind of discussion regarding Ruby on Rails
1322
- documentation is very welcome in the <a href="http://groups.google.com/group/rubyonrails-docs">rubyonrails-docs mailing list</a>.
1323
- </p>
1324
- </div>
1325
- </div>
1326
- </div>
1327
-
1328
- <hr class="hide" />
1329
- <div id="footer">
1330
- <div class="wrapper">
1331
- <p>This work is licensed under a <a href="https://creativecommons.org/licenses/by-sa/4.0/">Creative Commons Attribution-ShareAlike 4.0 International</a> License</p>
1332
- <p>"Rails", "Ruby on Rails", and the Rails logo are trademarks of David Heinemeier Hansson. All rights reserved.</p>
1333
-
1334
- </div>
1335
- </div>
1336
-
1337
- <script type="text/javascript" src="javascripts/jquery.min.js"></script>
1338
- <script type="text/javascript" src="javascripts/responsive-tables.js"></script>
1339
- <script type="text/javascript" src="javascripts/guides.js"></script>
1340
- <script type="text/javascript" src="javascripts/syntaxhighlighter/shCore.js"></script>
1341
- <script type="text/javascript" src="javascripts/syntaxhighlighter/shBrushRuby.js"></script>
1342
- <script type="text/javascript" src="javascripts/syntaxhighlighter/shBrushXml.js"></script>
1343
- <script type="text/javascript" src="javascripts/syntaxhighlighter/shBrushSql.js"></script>
1344
- <script type="text/javascript" src="javascripts/syntaxhighlighter/shBrushPlain.js"></script>
1345
- <script type="text/javascript">
1346
- SyntaxHighlighter.all();
1347
- $(guidesIndex.bind);
1348
- </script>
1349
- </body>
1350
- </html>