railties 3.0.11 → 3.0.12.rc1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (166) hide show
  1. data/lib/rails/version.rb +2 -2
  2. metadata +305 -461
  3. data/guides/output/2_2_release_notes.html +0 -565
  4. data/guides/output/2_3_release_notes.html +0 -713
  5. data/guides/output/3_0_release_notes.html +0 -652
  6. data/guides/output/3_1_release_notes.html +0 -670
  7. data/guides/output/action_controller_overview.html +0 -925
  8. data/guides/output/action_mailer_basics.html +0 -658
  9. data/guides/output/action_view_overview.html +0 -1471
  10. data/guides/output/active_model_basics.html +0 -349
  11. data/guides/output/active_record_basics.html +0 -364
  12. data/guides/output/active_record_querying.html +0 -1272
  13. data/guides/output/active_record_validations_callbacks.html +0 -1292
  14. data/guides/output/active_resource_basics.html +0 -252
  15. data/guides/output/active_support_core_extensions.html +0 -3374
  16. data/guides/output/ajax_on_rails.html +0 -412
  17. data/guides/output/api_documentation_guidelines.html +0 -317
  18. data/guides/output/asset_pipeline.html +0 -691
  19. data/guides/output/association_basics.html +0 -1742
  20. data/guides/output/caching_with_rails.html +0 -533
  21. data/guides/output/command_line.html +0 -662
  22. data/guides/output/configuring.html +0 -811
  23. data/guides/output/contribute.html +0 -216
  24. data/guides/output/contributing_to_ruby_on_rails.html +0 -465
  25. data/guides/output/credits.html +0 -210
  26. data/guides/output/debugging_rails_applications.html +0 -791
  27. data/guides/output/engines.html +0 -673
  28. data/guides/output/form_helpers.html +0 -850
  29. data/guides/output/generators.html +0 -725
  30. data/guides/output/getting_started.html +0 -1980
  31. data/guides/output/i18n.html +0 -1054
  32. data/guides/output/images/belongs_to.png +0 -0
  33. data/guides/output/images/book_icon.gif +0 -0
  34. data/guides/output/images/bullet.gif +0 -0
  35. data/guides/output/images/challenge.png +0 -0
  36. data/guides/output/images/chapters_icon.gif +0 -0
  37. data/guides/output/images/check_bullet.gif +0 -0
  38. data/guides/output/images/credits_pic_blank.gif +0 -0
  39. data/guides/output/images/csrf.png +0 -0
  40. data/guides/output/images/customized_error_messages.png +0 -0
  41. data/guides/output/images/edge_badge.png +0 -0
  42. data/guides/output/images/error_messages.png +0 -0
  43. data/guides/output/images/feature_tile.gif +0 -0
  44. data/guides/output/images/footer_tile.gif +0 -0
  45. data/guides/output/images/fxn.png +0 -0
  46. data/guides/output/images/grey_bullet.gif +0 -0
  47. data/guides/output/images/habtm.png +0 -0
  48. data/guides/output/images/has_many.png +0 -0
  49. data/guides/output/images/has_many_through.png +0 -0
  50. data/guides/output/images/has_one.png +0 -0
  51. data/guides/output/images/has_one_through.png +0 -0
  52. data/guides/output/images/header_backdrop.png +0 -0
  53. data/guides/output/images/header_tile.gif +0 -0
  54. data/guides/output/images/i18n/demo_html_safe.png +0 -0
  55. data/guides/output/images/i18n/demo_localized_pirate.png +0 -0
  56. data/guides/output/images/i18n/demo_translated_en.png +0 -0
  57. data/guides/output/images/i18n/demo_translated_pirate.png +0 -0
  58. data/guides/output/images/i18n/demo_translation_missing.png +0 -0
  59. data/guides/output/images/i18n/demo_untranslated.png +0 -0
  60. data/guides/output/images/icons/README +0 -5
  61. data/guides/output/images/icons/callouts/1.png +0 -0
  62. data/guides/output/images/icons/callouts/10.png +0 -0
  63. data/guides/output/images/icons/callouts/11.png +0 -0
  64. data/guides/output/images/icons/callouts/12.png +0 -0
  65. data/guides/output/images/icons/callouts/13.png +0 -0
  66. data/guides/output/images/icons/callouts/14.png +0 -0
  67. data/guides/output/images/icons/callouts/15.png +0 -0
  68. data/guides/output/images/icons/callouts/2.png +0 -0
  69. data/guides/output/images/icons/callouts/3.png +0 -0
  70. data/guides/output/images/icons/callouts/4.png +0 -0
  71. data/guides/output/images/icons/callouts/5.png +0 -0
  72. data/guides/output/images/icons/callouts/6.png +0 -0
  73. data/guides/output/images/icons/callouts/7.png +0 -0
  74. data/guides/output/images/icons/callouts/8.png +0 -0
  75. data/guides/output/images/icons/callouts/9.png +0 -0
  76. data/guides/output/images/icons/caution.png +0 -0
  77. data/guides/output/images/icons/example.png +0 -0
  78. data/guides/output/images/icons/home.png +0 -0
  79. data/guides/output/images/icons/important.png +0 -0
  80. data/guides/output/images/icons/next.png +0 -0
  81. data/guides/output/images/icons/note.png +0 -0
  82. data/guides/output/images/icons/prev.png +0 -0
  83. data/guides/output/images/icons/tip.png +0 -0
  84. data/guides/output/images/icons/up.png +0 -0
  85. data/guides/output/images/icons/warning.png +0 -0
  86. data/guides/output/images/jaimeiniesta.jpg +0 -0
  87. data/guides/output/images/nav_arrow.gif +0 -0
  88. data/guides/output/images/polymorphic.png +0 -0
  89. data/guides/output/images/posts_index.png +0 -0
  90. data/guides/output/images/radar.png +0 -0
  91. data/guides/output/images/rails_guides_logo.gif +0 -0
  92. data/guides/output/images/rails_logo_remix.gif +0 -0
  93. data/guides/output/images/rails_welcome.png +0 -0
  94. data/guides/output/images/session_fixation.png +0 -0
  95. data/guides/output/images/tab_grey.gif +0 -0
  96. data/guides/output/images/tab_info.gif +0 -0
  97. data/guides/output/images/tab_note.gif +0 -0
  98. data/guides/output/images/tab_red.gif +0 -0
  99. data/guides/output/images/tab_yellow.gif +0 -0
  100. data/guides/output/images/tab_yellow.png +0 -0
  101. data/guides/output/images/validation_error_messages.png +0 -0
  102. data/guides/output/images/vijaydev.jpg +0 -0
  103. data/guides/output/index.html +0 -300
  104. data/guides/output/initialization.html +0 -1087
  105. data/guides/output/javascripts/guides.js +0 -7
  106. data/guides/output/javascripts/syntaxhighlighter/shBrushAS3.js +0 -59
  107. data/guides/output/javascripts/syntaxhighlighter/shBrushAppleScript.js +0 -75
  108. data/guides/output/javascripts/syntaxhighlighter/shBrushBash.js +0 -59
  109. data/guides/output/javascripts/syntaxhighlighter/shBrushCSharp.js +0 -65
  110. data/guides/output/javascripts/syntaxhighlighter/shBrushColdFusion.js +0 -100
  111. data/guides/output/javascripts/syntaxhighlighter/shBrushCpp.js +0 -97
  112. data/guides/output/javascripts/syntaxhighlighter/shBrushCss.js +0 -91
  113. data/guides/output/javascripts/syntaxhighlighter/shBrushDelphi.js +0 -55
  114. data/guides/output/javascripts/syntaxhighlighter/shBrushDiff.js +0 -41
  115. data/guides/output/javascripts/syntaxhighlighter/shBrushErlang.js +0 -52
  116. data/guides/output/javascripts/syntaxhighlighter/shBrushGroovy.js +0 -67
  117. data/guides/output/javascripts/syntaxhighlighter/shBrushJScript.js +0 -52
  118. data/guides/output/javascripts/syntaxhighlighter/shBrushJava.js +0 -57
  119. data/guides/output/javascripts/syntaxhighlighter/shBrushJavaFX.js +0 -58
  120. data/guides/output/javascripts/syntaxhighlighter/shBrushPerl.js +0 -72
  121. data/guides/output/javascripts/syntaxhighlighter/shBrushPhp.js +0 -88
  122. data/guides/output/javascripts/syntaxhighlighter/shBrushPlain.js +0 -33
  123. data/guides/output/javascripts/syntaxhighlighter/shBrushPowerShell.js +0 -74
  124. data/guides/output/javascripts/syntaxhighlighter/shBrushPython.js +0 -64
  125. data/guides/output/javascripts/syntaxhighlighter/shBrushRuby.js +0 -55
  126. data/guides/output/javascripts/syntaxhighlighter/shBrushSass.js +0 -94
  127. data/guides/output/javascripts/syntaxhighlighter/shBrushScala.js +0 -51
  128. data/guides/output/javascripts/syntaxhighlighter/shBrushSql.js +0 -66
  129. data/guides/output/javascripts/syntaxhighlighter/shBrushVb.js +0 -56
  130. data/guides/output/javascripts/syntaxhighlighter/shBrushXml.js +0 -69
  131. data/guides/output/javascripts/syntaxhighlighter/shCore.js +0 -17
  132. data/guides/output/layout.html +0 -312
  133. data/guides/output/layouts_and_rendering.html +0 -1257
  134. data/guides/output/migrations.html +0 -751
  135. data/guides/output/nested_model_forms.html +0 -350
  136. data/guides/output/performance_testing.html +0 -858
  137. data/guides/output/plugins.html +0 -590
  138. data/guides/output/rails_application_templates.html +0 -368
  139. data/guides/output/rails_on_rack.html +0 -408
  140. data/guides/output/routing.html +0 -1246
  141. data/guides/output/ruby_on_rails_guides_guidelines.html +0 -218
  142. data/guides/output/security.html +0 -968
  143. data/guides/output/stylesheets/fixes.css +0 -16
  144. data/guides/output/stylesheets/main.css +0 -445
  145. data/guides/output/stylesheets/print.css +0 -52
  146. data/guides/output/stylesheets/reset.css +0 -43
  147. data/guides/output/stylesheets/style.css +0 -13
  148. data/guides/output/stylesheets/syntaxhighlighter/shCore.css +0 -226
  149. data/guides/output/stylesheets/syntaxhighlighter/shCoreDefault.css +0 -328
  150. data/guides/output/stylesheets/syntaxhighlighter/shCoreDjango.css +0 -331
  151. data/guides/output/stylesheets/syntaxhighlighter/shCoreEclipse.css +0 -339
  152. data/guides/output/stylesheets/syntaxhighlighter/shCoreEmacs.css +0 -324
  153. data/guides/output/stylesheets/syntaxhighlighter/shCoreFadeToGrey.css +0 -328
  154. data/guides/output/stylesheets/syntaxhighlighter/shCoreMDUltra.css +0 -324
  155. data/guides/output/stylesheets/syntaxhighlighter/shCoreMidnight.css +0 -324
  156. data/guides/output/stylesheets/syntaxhighlighter/shCoreRDark.css +0 -324
  157. data/guides/output/stylesheets/syntaxhighlighter/shThemeDefault.css +0 -117
  158. data/guides/output/stylesheets/syntaxhighlighter/shThemeDjango.css +0 -120
  159. data/guides/output/stylesheets/syntaxhighlighter/shThemeEclipse.css +0 -128
  160. data/guides/output/stylesheets/syntaxhighlighter/shThemeEmacs.css +0 -113
  161. data/guides/output/stylesheets/syntaxhighlighter/shThemeFadeToGrey.css +0 -117
  162. data/guides/output/stylesheets/syntaxhighlighter/shThemeMDUltra.css +0 -113
  163. data/guides/output/stylesheets/syntaxhighlighter/shThemeMidnight.css +0 -113
  164. data/guides/output/stylesheets/syntaxhighlighter/shThemeRDark.css +0 -113
  165. data/guides/output/stylesheets/syntaxhighlighter/shThemeRailsGuides.css +0 -116
  166. data/guides/output/testing.html +0 -1182
@@ -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,1182 +0,0 @@
1
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
2
- "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
3
-
4
- <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
5
- <head>
6
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
7
-
8
- <title>Ruby on Rails Guides: A Guide to Testing Rails Applications</title>
9
-
10
- <link rel="stylesheet" type="text/css" href="stylesheets/style.css" />
11
- <link rel="stylesheet" type="text/css" href="stylesheets/print.css" media="print" />
12
-
13
- <link rel="stylesheet" type="text/css" href="stylesheets/syntaxhighlighter/shCore.css" />
14
- <link rel="stylesheet" type="text/css" href="stylesheets/syntaxhighlighter/shThemeRailsGuides.css" />
15
-
16
- <link rel="stylesheet" type="text/css" href="stylesheets/fixes.css" />
17
- </head>
18
- <body class="guide">
19
- <div id="topNav">
20
- <div class="wrapper">
21
- <strong>More at <a href="http://rubyonrails.org/">rubyonrails.org:</a> </strong>
22
- <a href="http://rubyonrails.org/">Overview</a> |
23
- <a href="http://rubyonrails.org/download">Download</a> |
24
- <a href="http://rubyonrails.org/deploy">Deploy</a> |
25
- <a href="https://github.com/rails/rails">Code</a> |
26
- <a href="http://rubyonrails.org/screencasts">Screencasts</a> |
27
- <a href="http://rubyonrails.org/documentation">Documentation</a> |
28
- <a href="http://rubyonrails.org/ecosystem">Ecosystem</a> |
29
- <a href="http://rubyonrails.org/community">Community</a> |
30
- <a href="http://weblog.rubyonrails.org/">Blog</a>
31
- </div>
32
- </div>
33
- <div id="header">
34
- <div class="wrapper clearfix">
35
- <h1><a href="index.html" title="Return to home page">Guides.rubyonrails.org</a></h1>
36
- <p class="hide"><a href="#mainCol">Skip navigation</a>.</p>
37
- <ul class="nav">
38
- <li><a href="index.html">Home</a></li>
39
- <li class="index"><a href="index.html" onclick="guideMenu(); return false;" id="guidesMenu">Guides Index</a>
40
- <div id="guides" class="clearfix" style="display: none;">
41
- <hr />
42
- <dl class="L">
43
- <dt>Start Here</dt>
44
- <dd><a href="getting_started.html">Getting Started with Rails</a></dd>
45
- <dt>Models</dt>
46
- <dd><a href="migrations.html">Rails Database Migrations</a></dd>
47
- <dd><a href="active_record_validations_callbacks.html">Active Record Validations and Callbacks</a></dd>
48
- <dd><a href="association_basics.html">Active Record Associations</a></dd>
49
- <dd><a href="active_record_querying.html">Active Record Query Interface</a></dd>
50
- <dt>Views</dt>
51
- <dd><a href="layouts_and_rendering.html">Layouts and Rendering in Rails</a></dd>
52
- <dd><a href="form_helpers.html">Action View Form Helpers</a></dd>
53
- <dt>Controllers</dt>
54
- <dd><a href="action_controller_overview.html">Action Controller Overview</a></dd>
55
- <dd><a href="routing.html">Rails Routing from the Outside In</a></dd>
56
- </dl>
57
- <dl class="R">
58
- <dt>Digging Deeper</dt>
59
- <dd><a href="active_support_core_extensions.html">Active Support Core Extensions</a></dd>
60
- <dd><a href="i18n.html">Rails Internationalization API</a></dd>
61
- <dd><a href="action_mailer_basics.html">Action Mailer Basics</a></dd>
62
- <dd><a href="testing.html">Testing Rails Applications</a></dd>
63
- <dd><a href="security.html">Securing Rails Applications</a></dd>
64
- <dd><a href="debugging_rails_applications.html">Debugging Rails Applications</a></dd>
65
- <dd><a href="performance_testing.html">Performance Testing Rails Applications</a></dd>
66
- <dd><a href="configuring.html">Configuring Rails Applications</a></dd>
67
- <dd><a href="command_line.html">Rails Command Line Tools and Rake Tasks</a></dd>
68
- <dd><a href="caching_with_rails.html">Caching with Rails</a></dd>
69
- <dd><a href="asset_pipeline.html">Asset Pipeline</a></dd>
70
-
71
- <dt>Extending Rails</dt>
72
- <dd><a href="plugins.html">The Basics of Creating Rails Plugins</a></dd>
73
- <dd><a href="rails_on_rack.html">Rails on Rack</a></dd>
74
- <dd><a href="generators.html">Creating and Customizing Rails Generators</a></dd>
75
-
76
- <dt>Contributing to Ruby on Rails</dt>
77
- <dd><a href="contributing_to_ruby_on_rails.html">Contributing to Ruby on Rails</a></dd>
78
- <dd><a href="api_documentation_guidelines.html">API Documentation Guidelines</a></dd>
79
- <dd><a href="ruby_on_rails_guides_guidelines.html">Ruby on Rails Guides Guidelines</a></dd>
80
-
81
- <dt>Release Notes</dt>
82
- <dd><a href="3_1_release_notes.html">Ruby on Rails 3.1 Release Notes</a></dd>
83
- <dd><a href="3_0_release_notes.html">Ruby on Rails 3.0 Release Notes</a></dd>
84
- <dd><a href="2_3_release_notes.html">Ruby on Rails 2.3 Release Notes</a></dd>
85
- <dd><a href="2_2_release_notes.html">Ruby on Rails 2.2 Release Notes</a></dd>
86
- </dl>
87
- </div>
88
- </li>
89
- <li><a href="contributing_to_ruby_on_rails.html">Contribute</a></li>
90
- <li><a href="credits.html">Credits</a></li>
91
- </ul>
92
- </div>
93
- </div>
94
- <hr class="hide" />
95
-
96
- <div id="feature">
97
- <div class="wrapper">
98
- <h2>A Guide to Testing Rails Applications</h2>
99
- <p>This guide covers built-in mechanisms offered by Rails to test your application. By referring to this guide, you will be able to:</p>
100
- <ul>
101
- <li>Understand Rails testing terminology</li>
102
- <li>Write unit, functional and integration tests for your application</li>
103
- <li>Identify other popular testing approaches and plugins</li>
104
- </ul>
105
- <p>This guide won&#8217;t teach you to write a Rails application; it assumes basic familiarity with the Rails way of doing things.</p>
106
-
107
- <div id="subCol">
108
- <h3 class="chapter"><img src="images/chapters_icon.gif" alt="" />Chapters</h3>
109
- <ol class="chapters">
110
- <li><a href="#why-write-tests-for-your-rails-applications">Why Write Tests for your Rails Applications?</a></li><li><a href="#introduction-to-testing">Introduction to Testing</a><ul><li><a href="#the-three-environments">The Three Environments</a></li> <li><a href="#rails-sets-up-for-testing-from-the-word-go">Rails Sets up for Testing from the Word Go</a></li> <li><a href="#the-low-down-on-fixtures">The Low-Down on Fixtures</a></li></ul></li><li><a href="#unit-testing-your-models">Unit Testing your Models</a><ul><li><a href="#preparing-your-application-for-testing">Preparing your Application for Testing</a></li> <li><a href="#running-tests">Running Tests</a></li> <li><a href="#what-to-include-in-your-unit-tests">What to Include in Your Unit Tests</a></li> <li><a href="#assertions-available">Assertions Available</a></li> <li><a href="#rails-specific-assertions">Rails Specific Assertions</a></li></ul></li><li><a href="#functional-tests-for-your-controllers">Functional Tests for Your Controllers</a><ul><li><a href="#what-to-include-in-your-functional-tests">What to Include in your Functional Tests</a></li> <li><a href="#available-request-types-for-functional-tests">Available Request Types for Functional Tests</a></li> <li><a href="#the-four-hashes-of-the-apocalypse">The Four Hashes of the Apocalypse</a></li> <li><a href="#instance-variables-available">Instance Variables Available</a></li> <li><a href="#a-fuller-functional-test-example">A Fuller Functional Test Example</a></li> <li><a href="#testing-views">Testing Views</a></li></ul></li><li><a href="#integration-testing">Integration Testing</a><ul><li><a href="#helpers-available-for-integration-tests">Helpers Available for Integration Tests</a></li> <li><a href="#integration-testing-examples">Integration Testing Examples</a></li></ul></li><li><a href="#rake-tasks-for-running-your-tests">Rake Tasks for Running your Tests</a></li><li><a href="#brief-note-about-test-unit">Brief Note About <tt>Test::Unit</tt></a></li><li><a href="#setup-and-teardown">Setup and Teardown</a></li><li><a href="#testing-routes">Testing Routes</a></li><li><a href="#testing-your-mailers">Testing Your Mailers</a><ul><li><a href="#keeping-the-postman-in-check">Keeping the Postman in Check</a></li> <li><a href="#unit-testing">Unit Testing</a></li> <li><a href="#functional-testing">Functional Testing</a></li></ul></li><li><a href="#other-testing-approaches">Other Testing Approaches</a></li></ol></div>
111
- </div>
112
- </div>
113
-
114
- <div id="container">
115
- <div class="wrapper">
116
- <div id="mainCol">
117
- <h3 id="why-write-tests-for-your-rails-applications">1 Why Write Tests for your Rails Applications?</h3>
118
- <ul>
119
- <li>Rails makes it super easy to write your tests. It starts by producing skeleton test code in the background while you are creating your models and controllers.</li>
120
- <li>By simply running your Rails tests you can ensure your code adheres to the desired functionality even after some major code refactoring.</li>
121
- <li>Rails tests can also simulate browser requests and thus you can test your application&#8217;s response without having to test it through your browser.</li>
122
- </ul>
123
- <h3 id="introduction-to-testing">2 Introduction to Testing</h3>
124
- <p>Testing support was woven into the Rails fabric from the beginning. It wasn&#8217;t an &#8220;oh! let&#8217;s bolt on support for running tests because they&#8217;re new and cool&#8221; epiphany. Just about every Rails application interacts heavily with a database &#8211; and, as a result, your tests will need a database to interact with as well. To write efficient tests, you&#8217;ll need to understand how to set up this database and populate it with sample data.</p>
125
- <h4 id="the-three-environments">2.1 The Three Environments</h4>
126
- <p>Every Rails application you build has 3 sides: a side for production, a side for development, and a side for testing.</p>
127
- <p>One place you&#8217;ll find this distinction is in the <tt>config/database.yml</tt> file. This <span class="caps">YAML</span> configuration file has 3 different sections defining 3 unique database setups:</p>
128
- <ul>
129
- <li>production</li>
130
- <li>development</li>
131
- <li>test</li>
132
- </ul>
133
- <p>This allows you to set up and interact with test data without any danger of your tests altering data from your production environment.</p>
134
- <p>For example, suppose you need to test your new <tt>delete_this_user_and_every_everything_associated_with_it</tt> function. Wouldn&#8217;t you want to run this in an environment where it makes no difference if you destroy data or not?</p>
135
- <p>When you do end up destroying your testing database (and it will happen, trust me), you can rebuild it from scratch according to the specs defined in the development database. You can do this by running <tt>rake db:test:prepare</tt>.</p>
136
- <h4 id="rails-sets-up-for-testing-from-the-word-go">2.2 Rails Sets up for Testing from the Word Go</h4>
137
- <p>Rails creates a <tt>test</tt> folder for you as soon as you create a Rails project using <tt>rails new</tt> <em>application_name</em>. If you list the contents of this folder then you shall see:</p>
138
- <div class="code_container">
139
- <pre class="brush: plain; gutter: false; toolbar: false">
140
- $ ls -F test/
141
-
142
- fixtures/ functional/ integration/ test_helper.rb unit/
143
- </pre>
144
- </div>
145
- <p>The <tt>unit</tt> folder is meant to hold tests for your models, the <tt>functional</tt> folder is meant to hold tests for your controllers, and the <tt>integration</tt> folder is meant to hold tests that involve any number of controllers interacting. Fixtures are a way of organizing test data; they reside in the <tt>fixtures</tt> folder. The <tt>test_helper.rb</tt> file holds the default configuration for your tests.</p>
146
- <h4 id="the-low-down-on-fixtures">2.3 The Low-Down on Fixtures</h4>
147
- <p>For good tests, you&#8217;ll need to give some thought to setting up test data. In Rails, you can handle this by defining and customizing fixtures.</p>
148
- <h5 id="what-are-fixtures">2.3.1 What are Fixtures?</h5>
149
- <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 and assume a single format: <strong><span class="caps">YAML</span></strong>.</p>
150
- <p>You&#8217;ll find fixtures under your <tt>test/fixtures</tt> directory. When you run <tt>rails generate model</tt> to create a new model, fixture stubs will be automatically created and placed in this directory.</p>
151
- <h5 id="yaml">2.3.2 <span class="caps">YAML</span></h5>
152
- <p><span class="caps">YAML</span>-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 <tt>users.yml</tt>).</p>
153
- <p>Here&#8217;s a sample <span class="caps">YAML</span> fixture file:</p>
154
- <div class="code_container">
155
- <pre class="brush: plain; gutter: false; toolbar: false">
156
- # lo &amp; behold! I am a YAML comment!
157
- david:
158
- name: David Heinemeier Hansson
159
- birthday: 1979-10-15
160
- profession: Systems development
161
-
162
- steve:
163
- name: Steve Ross Kellock
164
- birthday: 1974-09-27
165
- profession: guy with keyboard
166
- </pre>
167
- </div>
168
- <p>Each fixture is given a name followed by an indented list of colon-separated key/value pairs. Records are separated by a blank space. You can place comments in a fixture file by using the # character in the first column.</p>
169
- <h5 id="erb-in-it-up">2.3.3 ERB&#8217;in It Up</h5>
170
- <p><span class="caps">ERB</span> allows you to embed ruby code within templates. <span class="caps">YAML</span> fixture format is pre-processed with <span class="caps">ERB</span> when you load fixtures. This allows you to use Ruby to help you generate some sample data.</p>
171
- <div class="code_container">
172
- <pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
173
- &lt;% earth_size = 20 %&gt;
174
- mercury:
175
- size: &lt;%= earth_size / 50 %&gt;
176
- brightest_on: &lt;%= 113.days.ago.to_s(:db) %&gt;
177
-
178
- venus:
179
- size: &lt;%= earth_size / 2 %&gt;
180
- brightest_on: &lt;%= 67.days.ago.to_s(:db) %&gt;
181
-
182
- mars:
183
- size: &lt;%= earth_size - 69 %&gt;
184
- brightest_on: &lt;%= 13.days.from_now.to_s(:db) %&gt;
185
- </pre>
186
- </div>
187
- <p>Anything encased within the</p>
188
- <div class="code_container">
189
- <pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
190
- &lt;% %&gt;
191
- </pre>
192
- </div>
193
- <p>tag is considered Ruby code. When this fixture is loaded, the <tt>size</tt> attribute of the three records will be set to 20/50, 20/2, and 20-69 respectively. The <tt>brightest_on</tt> attribute will also be evaluated and formatted by Rails to be compatible with the database.</p>
194
- <h5 id="fixtures-in-action">2.3.4 Fixtures in Action</h5>
195
- <p>Rails by default automatically loads all fixtures from the <tt>test/fixtures</tt> folder for your unit and functional test. Loading involves three steps:</p>
196
- <ul>
197
- <li>Remove any existing data from the table corresponding to the fixture</li>
198
- <li>Load the fixture data into the table</li>
199
- <li>Dump the fixture data into a variable in case you want to access it directly</li>
200
- </ul>
201
- <h5 id="hashes-with-special-powers">2.3.5 Hashes with Special Powers</h5>
202
- <p>Fixtures are basically Hash objects. As mentioned in point #3 above, you can access the hash object directly because it is automatically setup as a local variable of the test case. For example:</p>
203
- <div class="code_container">
204
- <pre class="brush: ruby; gutter: false; toolbar: false">
205
- # this will return the Hash for the fixture named david
206
- users(:david)
207
-
208
- # this will return the property for david called id
209
- users(:david).id
210
- </pre>
211
- </div>
212
- <p>Fixtures can also transform themselves into the form of the original class. Thus, you can get at the methods only available to that class.</p>
213
- <div class="code_container">
214
- <pre class="brush: ruby; gutter: false; toolbar: false">
215
- # using the find method, we grab the &quot;real&quot; david as a User
216
- david = users(:david).find
217
-
218
- # and now we have access to methods only available to a User class
219
- email(david.girlfriend.email, david.location_tonight)
220
- </pre>
221
- </div>
222
- <h3 id="unit-testing-your-models">3 Unit Testing your Models</h3>
223
- <p>In Rails, unit tests are what you write to test your models.</p>
224
- <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. I will be using examples from this generated code and will be supplementing it with additional examples where necessary.</p>
225
- <div class="note"><p>For more information on Rails <i>scaffolding</i>, refer to <a href="getting_started.html">Getting Started with Rails</a></p></div>
226
- <p>When you use <tt>rails generate scaffold</tt>, for a resource among other things it creates a test stub in the <tt>test/unit</tt> folder:</p>
227
- <div class="code_container">
228
- <pre class="brush: plain; gutter: false; toolbar: false">
229
- $ rails generate scaffold post title:string body:text
230
- ...
231
- create app/models/post.rb
232
- create test/unit/post_test.rb
233
- create test/fixtures/posts.yml
234
- ...
235
- </pre>
236
- </div>
237
- <p>The default test stub in <tt>test/unit/post_test.rb</tt> looks like this:</p>
238
- <div class="code_container">
239
- <pre class="brush: ruby; gutter: false; toolbar: false">
240
- require 'test_helper'
241
-
242
- class PostTest &lt; ActiveSupport::TestCase
243
- # Replace this with your real tests.
244
- test &quot;the truth&quot; do
245
- assert true
246
- end
247
- end
248
- </pre>
249
- </div>
250
- <p>A line by line examination of this file will help get you oriented to Rails testing code and terminology.</p>
251
- <div class="code_container">
252
- <pre class="brush: ruby; gutter: false; toolbar: false">
253
- require 'test_helper'
254
- </pre>
255
- </div>
256
- <p>As you know by now, <tt>test_helper.rb</tt> 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>
257
- <div class="code_container">
258
- <pre class="brush: ruby; gutter: false; toolbar: false">
259
- class PostTest &lt; ActiveSupport::TestCase
260
- </pre>
261
- </div>
262
- <p>The <tt>PostTest</tt> class defines a <em>test case</em> because it inherits from <tt>ActiveSupport::TestCase</tt>. <tt>PostTest</tt> thus has all the methods available from <tt>ActiveSupport::TestCase</tt>. You&#8217;ll see those methods a little later in this guide.</p>
263
- <p>Any method defined within a <tt>Test::Unit</tt> test case that begins with <tt>test</tt> (case sensitive) is simply called a test. So, <tt>test_password</tt>, <tt>test_valid_password</tt> and <tt>testValidPassword</tt> all are legal test names and are run automatically when the test case is run.</p>
264
- <p>Rails adds a <tt>test</tt> method that takes a test name and a block. It generates a normal <tt>Test::Unit</tt> test with method names prefixed with <tt>test_</tt>. So,</p>
265
- <div class="code_container">
266
- <pre class="brush: ruby; gutter: false; toolbar: false">
267
- test &quot;the truth&quot; do
268
- assert true
269
- end
270
- </pre>
271
- </div>
272
- <p>acts as if you had written</p>
273
- <div class="code_container">
274
- <pre class="brush: ruby; gutter: false; toolbar: false">
275
- def test_the_truth
276
- assert true
277
- end
278
- </pre>
279
- </div>
280
- <p>only the <tt>test</tt> macro allows a more readable test name. You can still use regular method definitions though.</p>
281
- <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&#8217;s because in Ruby technically any string may be a method name. Odd ones need <tt>define_method</tt> and <tt>send</tt> calls, but formally there&#8217;s no restriction.</p></div>
282
- <div class="code_container">
283
- <pre class="brush: ruby; gutter: false; toolbar: false">
284
- assert true
285
- </pre>
286
- </div>
287
- <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>
288
- <ul>
289
- <li>does this value = that value?</li>
290
- <li>is this object nil?</li>
291
- <li>does this line of code throw an exception?</li>
292
- <li>is the user&#8217;s password greater than 5 characters?</li>
293
- </ul>
294
- <p>Every test contains one or more assertions. Only when all the assertions are successful will the test pass.</p>
295
- <h4 id="preparing-your-application-for-testing">3.1 Preparing your Application for Testing</h4>
296
- <p>Before you can run your tests, you need to ensure that the test database structure is current. For this you can use the following rake commands:</p>
297
- <div class="code_container">
298
- <pre class="brush: plain; gutter: false; toolbar: false">
299
- $ rake db:migrate
300
- ...
301
- $ rake db:test:load
302
- </pre>
303
- </div>
304
- <p>The <tt>rake db:migrate</tt> above runs any pending migrations on the <em>development</em> environment and updates <tt>db/schema.rb</tt>. The <tt>rake db:test:load</tt> recreates the test database from the current <tt>db/schema.rb</tt>. On subsequent attempts, it is a good idea to first run <tt>db:test:prepare</tt>, as it first checks for pending migrations and warns you appropriately.</p>
305
- <div class="note"><p><tt>db:test:prepare</tt> will fail with an error if <tt>db/schema.rb</tt> doesn&#8217;t exist.</p></div>
306
- <h5 id="rake-tasks-for-preparing-your-application-for-testing">3.1.1 Rake Tasks for Preparing your Application for Testing</h5>
307
- <table>
308
- <tr>
309
- <th>Tasks </th>
310
- <th>Description</th>
311
- </tr>
312
- <tr>
313
- <td><tt>rake db:test:clone</tt> </td>
314
- <td>Recreate the test database from the current environment&#8217;s database schema</td>
315
- </tr>
316
- <tr>
317
- <td><tt>rake db:test:clone_structure</tt> </td>
318
- <td>Recreate the test database from the development structure</td>
319
- </tr>
320
- <tr>
321
- <td><tt>rake db:test:load</tt> </td>
322
- <td>Recreate the test database from the current <tt>schema.rb</tt></td>
323
- </tr>
324
- <tr>
325
- <td><tt>rake db:test:prepare</tt> </td>
326
- <td>Check for pending migrations and load the test schema</td>
327
- </tr>
328
- <tr>
329
- <td><tt>rake db:test:purge</tt> </td>
330
- <td>Empty the test database.</td>
331
- </tr>
332
- </table>
333
- <div class="info"><p>You can see all these rake tasks and their descriptions by running <tt>rake --tasks --describe</tt></p></div>
334
- <h4 id="running-tests">3.2 Running Tests</h4>
335
- <p>Running a test is as simple as invoking the file containing the test cases through Ruby:</p>
336
- <div class="code_container">
337
- <pre class="brush: plain; gutter: false; toolbar: false">
338
- $ ruby -Itest test/unit/post_test.rb
339
-
340
- Loaded suite unit/post_test
341
- Started
342
- .
343
- Finished in 0.023513 seconds.
344
-
345
- 1 tests, 1 assertions, 0 failures, 0 errors
346
- </pre>
347
- </div>
348
- <p>This will run all the test methods from the test case. Note that <tt>test_helper.rb</tt> is in the <tt>test</tt> directory, hence this directory needs to be added to the load path using the <tt>-I</tt> switch.</p>
349
- <p>You can also run a particular test method from the test case by using the <tt>-n</tt> switch with the <tt>test method name</tt>.</p>
350
- <div class="code_container">
351
- <pre class="brush: plain; gutter: false; toolbar: false">
352
- $ ruby -Itest test/unit/post_test.rb -n test_the_truth
353
-
354
- Loaded suite unit/post_test
355
- Started
356
- .
357
- Finished in 0.023513 seconds.
358
-
359
- 1 tests, 1 assertions, 0 failures, 0 errors
360
- </pre>
361
- </div>
362
- <p>The <tt>.</tt> (dot) above indicates a passing test. When a test fails you see an <tt>F</tt>; when a test throws an error you see an <tt>E</tt> in its place. The last line of the output is the summary.</p>
363
- <p>To see how a test failure is reported, you can add a failing test to the <tt>post_test.rb</tt> test case.</p>
364
- <div class="code_container">
365
- <pre class="brush: ruby; gutter: false; toolbar: false">
366
- test &quot;should not save post without title&quot; do
367
- post = Post.new
368
- assert !post.save
369
- end
370
- </pre>
371
- </div>
372
- <p>Let us run this newly added test.</p>
373
- <div class="code_container">
374
- <pre class="brush: plain; gutter: false; toolbar: false">
375
- $ ruby unit/post_test.rb -n test_should_not_save_post_without_title
376
- Loaded suite -e
377
- Started
378
- F
379
- Finished in 0.102072 seconds.
380
-
381
- 1) Failure:
382
- test_should_not_save_post_without_title(PostTest) [/test/unit/post_test.rb:6]:
383
- &lt;false&gt; is not true.
384
-
385
- 1 tests, 1 assertions, 1 failures, 0 errors
386
- </pre>
387
- </div>
388
- <p>In the output, <tt>F</tt> denotes a failure. You can see the corresponding trace shown under <tt>1)</tt> 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>
389
- <div class="code_container">
390
- <pre class="brush: ruby; gutter: false; toolbar: false">
391
- test &quot;should not save post without title&quot; do
392
- post = Post.new
393
- assert !post.save, &quot;Saved the post without a title&quot;
394
- end
395
- </pre>
396
- </div>
397
- <p>Running this test shows the friendlier assertion message:</p>
398
- <div class="code_container">
399
- <pre class="brush: plain; gutter: false; toolbar: false">
400
- 1) Failure:
401
- test_should_not_save_post_without_title(PostTest) [/test/unit/post_test.rb:6]:
402
- Saved the post without a title.
403
- &lt;false&gt; is not true.
404
- </pre>
405
- </div>
406
- <p>Now to get this test to pass we can add a model level validation for the <em>title</em> field.</p>
407
- <div class="code_container">
408
- <pre class="brush: ruby; gutter: false; toolbar: false">
409
- class Post &lt; ActiveRecord::Base
410
- validates :title, :presence =&gt; true
411
- end
412
- </pre>
413
- </div>
414
- <p>Now the test should pass. Let us verify by running the test again:</p>
415
- <div class="code_container">
416
- <pre class="brush: plain; gutter: false; toolbar: false">
417
- $ ruby unit/post_test.rb -n test_should_not_save_post_without_title
418
- Loaded suite unit/post_test
419
- Started
420
- .
421
- Finished in 0.193608 seconds.
422
-
423
- 1 tests, 1 assertions, 0 failures, 0 errors
424
- </pre>
425
- </div>
426
- <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> (<span class="caps">TDD</span>).</p>
427
- <div class="info"><p>Many Rails developers practice <em>Test-Driven Development</em> (<span class="caps">TDD</span>). This is an excellent way to build up a test suite that exercises every part of your application. <span class="caps">TDD</span> 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 <span class="caps">TDD</span> steps to create a Rails application</a>.</p></div>
428
- <p>To see how an error gets reported, here&#8217;s a test containing an error:</p>
429
- <div class="code_container">
430
- <pre class="brush: ruby; gutter: false; toolbar: false">
431
- test &quot;should report error&quot; do
432
- # some_undefined_variable is not defined elsewhere in the test case
433
- some_undefined_variable
434
- assert true
435
- end
436
- </pre>
437
- </div>
438
- <p>Now you can see even more output in the console from running the tests:</p>
439
- <div class="code_container">
440
- <pre class="brush: plain; gutter: false; toolbar: false">
441
- $ ruby unit/post_test.rb -n test_should_report_error
442
- Loaded suite -e
443
- Started
444
- E
445
- Finished in 0.082603 seconds.
446
-
447
- 1) Error:
448
- test_should_report_error(PostTest):
449
- NameError: undefined local variable or method `some_undefined_variable' for #&lt;PostTest:0x249d354&gt;
450
- /test/unit/post_test.rb:6:in `test_should_report_error'
451
-
452
- 1 tests, 0 assertions, 0 failures, 1 errors
453
- </pre>
454
- </div>
455
- <p>Notice the &#8216;E&#8217; in the output. It denotes a test with error.</p>
456
- <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>
457
- <h4 id="what-to-include-in-your-unit-tests">3.3 What to Include in Your Unit Tests</h4>
458
- <p>Ideally, you would like to include a test for everything which could possibly break. It&#8217;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>
459
- <h4 id="assertions-available">3.4 Assertions Available</h4>
460
- <p>By now you&#8217;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>
461
- <p>There are a bunch of different types of assertions you can use. Here&#8217;s the complete list of assertions that ship with <tt>test/unit</tt>, the default testing library used by Rails. The <tt>[msg]</tt> parameter is an optional string message you can specify to make your test failure messages clearer. It&#8217;s not required.</p>
462
- <table>
463
- <tr>
464
- <th>Assertion </th>
465
- <th>Purpose</th>
466
- </tr>
467
- <tr>
468
- <td><tt>assert( boolean, [msg] )</tt> </td>
469
- <td>Ensures that the object/expression is true.</td>
470
- </tr>
471
- <tr>
472
- <td><tt>assert_equal( obj1, obj2, [msg] )</tt> </td>
473
- <td>Ensures that <tt>obj1 == obj2</tt> is true.</td>
474
- </tr>
475
- <tr>
476
- <td><tt>assert_not_equal( obj1, obj2, [msg] )</tt> </td>
477
- <td>Ensures that <tt>obj1 == obj2</tt> is false.</td>
478
- </tr>
479
- <tr>
480
- <td><tt>assert_same( obj1, obj2, [msg] )</tt> </td>
481
- <td>Ensures that <tt>obj1.equal?(obj2)</tt> is true.</td>
482
- </tr>
483
- <tr>
484
- <td><tt>assert_not_same( obj1, obj2, [msg] )</tt> </td>
485
- <td>Ensures that <tt>obj1.equal?(obj2)</tt> is false.</td>
486
- </tr>
487
- <tr>
488
- <td><tt>assert_nil( obj, [msg] )</tt> </td>
489
- <td>Ensures that <tt>obj.nil?</tt> is true.</td>
490
- </tr>
491
- <tr>
492
- <td><tt>assert_not_nil( obj, [msg] )</tt> </td>
493
- <td>Ensures that <tt>obj.nil?</tt> is false.</td>
494
- </tr>
495
- <tr>
496
- <td><tt>assert_match( regexp, string, [msg] )</tt> </td>
497
- <td>Ensures that a string matches the regular expression.</td>
498
- </tr>
499
- <tr>
500
- <td><tt>assert_no_match( regexp, string, [msg] )</tt> </td>
501
- <td>Ensures that a string doesn&#8217;t match the regular expression.</td>
502
- </tr>
503
- <tr>
504
- <td><tt>assert_in_delta( expecting, actual, delta, [msg] )</tt> </td>
505
- <td>Ensures that the numbers <tt>expecting</tt> and <tt>actual</tt> are within <tt>delta</tt> of each other.</td>
506
- </tr>
507
- <tr>
508
- <td><tt>assert_throws( symbol, [msg] ) { block }</tt> </td>
509
- <td>Ensures that the given block throws the symbol.</td>
510
- </tr>
511
- <tr>
512
- <td><tt>assert_raise( exception1, exception2, ... ) { block }</tt> </td>
513
- <td>Ensures that the given block raises one of the given exceptions.</td>
514
- </tr>
515
- <tr>
516
- <td><tt>assert_nothing_raised( exception1, exception2, ... ) { block }</tt> </td>
517
- <td>Ensures that the given block doesn&#8217;t raise one of the given exceptions.</td>
518
- </tr>
519
- <tr>
520
- <td><tt>assert_instance_of( class, obj, [msg] )</tt> </td>
521
- <td>Ensures that <tt>obj</tt> is of the <tt>class</tt> type.</td>
522
- </tr>
523
- <tr>
524
- <td><tt>assert_kind_of( class, obj, [msg] )</tt> </td>
525
- <td>Ensures that <tt>obj</tt> is or descends from <tt>class</tt>.</td>
526
- </tr>
527
- <tr>
528
- <td><tt>assert_respond_to( obj, symbol, [msg] )</tt> </td>
529
- <td>Ensures that <tt>obj</tt> has a method called <tt>symbol</tt>.</td>
530
- </tr>
531
- <tr>
532
- <td><tt>assert_operator( obj1, operator, obj2, [msg] )</tt> </td>
533
- <td>Ensures that <tt>obj1.operator(obj2)</tt> is true.</td>
534
- </tr>
535
- <tr>
536
- <td><tt>assert_send( array, [msg] )</tt> </td>
537
- <td>Ensures that executing the method listed in <tt>array[1]</tt> on the object in <tt>array[0]</tt> with the parameters of <tt>array[2 and up]</tt> is true. This one is weird eh?</td>
538
- </tr>
539
- <tr>
540
- <td><tt>flunk( [msg] )</tt> </td>
541
- <td>Ensures failure. This is useful to explicitly mark a test that isn&#8217;t finished yet.</td>
542
- </tr>
543
- </table>
544
- <p>Because of the modular nature of the testing framework, it is possible to create your own assertions. In fact, that&#8217;s exactly what Rails does. It includes some specialized assertions to make your life easier.</p>
545
- <div class="note"><p>Creating your own assertions is an advanced topic that we won&#8217;t cover in this tutorial.</p></div>
546
- <h4 id="rails-specific-assertions">3.5 Rails Specific Assertions</h4>
547
- <p>Rails adds some custom assertions of its own to the <tt>test/unit</tt> framework:</p>
548
- <div class="note"><p><tt>assert_valid(record)</tt> has been deprecated. Please use <tt>assert(record.valid?)</tt> instead.</p></div>
549
- <table>
550
- <tr>
551
- <th>Assertion </th>
552
- <th>Purpose</th>
553
- </tr>
554
- <tr>
555
- <td><tt>assert_valid(record)</tt> </td>
556
- <td>Ensures that the passed record is valid by Active Record standards and returns any error messages if it is not.</td>
557
- </tr>
558
- <tr>
559
- <td><tt>assert_difference(expressions, difference = 1, message = nil) {...}</tt> </td>
560
- <td>Test numeric difference between the return value of an expression as a result of what is evaluated in the yielded block.</td>
561
- </tr>
562
- <tr>
563
- <td><tt>assert_no_difference(expressions, message = nil, &amp;block)</tt> </td>
564
- <td>Asserts that the numeric result of evaluating an expression is not changed before and after invoking the passed in block.</td>
565
- </tr>
566
- <tr>
567
- <td><tt>assert_recognizes(expected_options, path, extras={}, message=nil)</tt> </td>
568
- <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>
569
- </tr>
570
- <tr>
571
- <td><tt>assert_generates(expected_path, options, defaults={}, extras = {}, message=nil)</tt> </td>
572
- <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>
573
- </tr>
574
- <tr>
575
- <td><tt>assert_response(type, message = nil)</tt> </td>
576
- <td>Asserts that the response comes with a specific status code. You can specify <tt>:success</tt> to indicate 200, <tt>:redirect</tt> to indicate 300-399, <tt>:missing</tt> to indicate 404, or <tt>:error</tt> to match the 500-599 range</td>
577
- </tr>
578
- <tr>
579
- <td><tt>assert_redirected_to(options = {}, message=nil)</tt> </td>
580
- <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 <tt>assert_redirected_to(:controller => "weblog")</tt> will also match the redirection of <tt>redirect_to(:controller => "weblog", :action => "show")</tt> and so on.</td>
581
- </tr>
582
- <tr>
583
- <td><tt>assert_template(expected = nil, message=nil)</tt> </td>
584
- <td>Asserts that the request was rendered with the appropriate template file.</td>
585
- </tr>
586
- </table>
587
- <p>You&#8217;ll see the usage of some of these assertions in the next chapter.</p>
588
- <h3 id="functional-tests-for-your-controllers">4 Functional Tests for Your Controllers</h3>
589
- <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>
590
- <h4 id="what-to-include-in-your-functional-tests">4.1 What to Include in your Functional Tests</h4>
591
- <p>You should test for things such as:</p>
592
- <ul>
593
- <li>was the web request successful?</li>
594
- <li>was the user redirected to the right page?</li>
595
- <li>was the user successfully authenticated?</li>
596
- <li>was the correct object stored in the response template?</li>
597
- <li>was the appropriate message displayed to the user in the view?</li>
598
- </ul>
599
- <p>Now that we have used Rails scaffold generator for our <tt>Post</tt> resource, it has already created the controller code and functional tests. You can take look at the file <tt>posts_controller_test.rb</tt> in the <tt>test/functional</tt> directory.</p>
600
- <p>Let me take you through one such test, <tt>test_should_get_index</tt> from the file <tt>posts_controller_test.rb</tt>.</p>
601
- <div class="code_container">
602
- <pre class="brush: ruby; gutter: false; toolbar: false">
603
- test &quot;should get index&quot; do
604
- get :index
605
- assert_response :success
606
- assert_not_nil assigns(:posts)
607
- end
608
- </pre>
609
- </div>
610
- <p>In the <tt>test_should_get_index</tt> test, Rails simulates a request on the action called <tt>index</tt>, making sure the request was successful and also ensuring that it assigns a valid <tt>posts</tt> instance variable.</p>
611
- <p>The <tt>get</tt> method kicks off the web request and populates the results into the response. It accepts 4 arguments:</p>
612
- <ul>
613
- <li>The action of the controller you are requesting. This can be in the form of a string or a symbol.</li>
614
- <li>An optional hash of request parameters to pass into the action (eg. query string parameters or post variables).</li>
615
- <li>An optional hash of session variables to pass along with the request.</li>
616
- <li>An optional hash of flash values.</li>
617
- </ul>
618
- <p>Example: Calling the <tt>:show</tt> action, passing an <tt>id</tt> of 12 as the <tt>params</tt> and setting a <tt>user_id</tt> of 5 in the session:</p>
619
- <div class="code_container">
620
- <pre class="brush: ruby; gutter: false; toolbar: false">
621
- get(:show, {'id' =&gt; &quot;12&quot;}, {'user_id' =&gt; 5})
622
- </pre>
623
- </div>
624
- <p>Another example: Calling the <tt>:view</tt> action, passing an <tt>id</tt> of 12 as the <tt>params</tt>, this time with no session, but with a flash message.</p>
625
- <div class="code_container">
626
- <pre class="brush: ruby; gutter: false; toolbar: false">
627
- get(:view, {'id' =&gt; '12'}, nil, {'message' =&gt; 'booya!'})
628
- </pre>
629
- </div>
630
- <div class="note"><p>If you try running <tt>test_should_create_post</tt> test from <tt>posts_controller_test.rb</tt> it will fail on account of the newly added model level validation and rightly so.</p></div>
631
- <p>Let us modify <tt>test_should_create_post</tt> test in <tt>posts_controller_test.rb</tt> so that all our test pass:</p>
632
- <div class="code_container">
633
- <pre class="brush: ruby; gutter: false; toolbar: false">
634
- test &quot;should create post&quot; do
635
- assert_difference('Post.count') do
636
- post :create, :post =&gt; { :title =&gt; 'Some title'}
637
- end
638
-
639
- assert_redirected_to post_path(assigns(:post))
640
- end
641
- </pre>
642
- </div>
643
- <p>Now you can try running all the tests and they should pass.</p>
644
- <h4 id="available-request-types-for-functional-tests">4.2 Available Request Types for Functional Tests</h4>
645
- <p>If you&#8217;re familiar with the <span class="caps">HTTP</span> protocol, you&#8217;ll know that <tt>get</tt> is a type of request. There are 5 request types supported in Rails functional tests:</p>
646
- <ul>
647
- <li><tt>get</tt></li>
648
- <li><tt>post</tt></li>
649
- <li><tt>put</tt></li>
650
- <li><tt>head</tt></li>
651
- <li><tt>delete</tt></li>
652
- </ul>
653
- <p>All of request types are methods that you can use, however, you&#8217;ll probably end up using the first two more often than the others.</p>
654
- <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>
655
- <h4 id="the-four-hashes-of-the-apocalypse">4.3 The Four Hashes of the Apocalypse</h4>
656
- <p>After a request has been made by using one of the 5 methods (<tt>get</tt>, <tt>post</tt>, etc.) and processed, you will have 4 Hash objects ready for use:</p>
657
- <ul>
658
- <li><tt>assigns</tt> &#8211; Any objects that are stored as instance variables in actions for use in views.</li>
659
- <li><tt>cookies</tt> &#8211; Any cookies that are set.</li>
660
- <li><tt>flash</tt> &#8211; Any objects living in the flash.</li>
661
- <li><tt>session</tt> &#8211; Any object living in session variables.</li>
662
- </ul>
663
- <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 <tt>assigns</tt>. For example:</p>
664
- <div class="code_container">
665
- <pre class="brush: ruby; gutter: false; toolbar: false">
666
- flash[&quot;gordon&quot;] flash[:gordon]
667
- session[&quot;shmession&quot;] session[:shmession]
668
- cookies[&quot;are_good_for_u&quot;] cookies[:are_good_for_u]
669
-
670
- # Because you can't use assigns[:something] for historical reasons:
671
- assigns[&quot;something&quot;] assigns(:something)
672
- </pre>
673
- </div>
674
- <h4 id="instance-variables-available">4.4 Instance Variables Available</h4>
675
- <p>You also have access to three instance variables in your functional tests:</p>
676
- <ul>
677
- <li><tt>@controller</tt> &#8211; The controller processing the request</li>
678
- <li><tt>@request</tt> &#8211; The request</li>
679
- <li><tt>@response</tt> &#8211; The response</li>
680
- </ul>
681
- <h4 id="a-fuller-functional-test-example">4.5 A Fuller Functional Test Example</h4>
682
- <p>Here&#8217;s another example that uses <tt>flash</tt>, <tt>assert_redirected_to</tt>, and <tt>assert_difference</tt>:</p>
683
- <div class="code_container">
684
- <pre class="brush: ruby; gutter: false; toolbar: false">
685
- test &quot;should create post&quot; do
686
- assert_difference('Post.count') do
687
- post :create, :post =&gt; { :title =&gt; 'Hi', :body =&gt; 'This is my first post.'}
688
- end
689
- assert_redirected_to post_path(assigns(:post))
690
- assert_equal 'Post was successfully created.', flash[:notice]
691
- end
692
- </pre>
693
- </div>
694
- <h4 id="testing-views">4.6 Testing Views</h4>
695
- <p>Testing the response to your request by asserting the presence of key <span class="caps">HTML</span> elements and their content is a useful way to test the views of your application. The <tt>assert_select</tt> assertion allows you to do this by using a simple yet powerful syntax.</p>
696
- <div class="note"><p>You may find references to <tt>assert_tag</tt> in other documentation, but this is now deprecated in favor of <tt>assert_select</tt>.</p></div>
697
- <p>There are two forms of <tt>assert_select</tt>:</p>
698
- <p><tt>assert_select(selector, [equality], [message])</tt> ensures that the equality condition is met on the selected elements through the selector. The selector may be a <span class="caps">CSS</span> selector expression (String), an expression with substitution values, or an <tt>HTML::Selector</tt> object.</p>
699
- <p><tt>assert_select(element, selector, [equality], [message])</tt> ensures that the equality condition is met on all the selected elements through the selector starting from the <em>element</em> (instance of <tt>HTML::Node</tt>) and its descendants.</p>
700
- <p>For example, you could verify the contents on the title element in your response with:</p>
701
- <div class="code_container">
702
- <pre class="brush: ruby; gutter: false; toolbar: false">
703
- assert_select 'title', &quot;Welcome to Rails Testing Guide&quot;
704
- </pre>
705
- </div>
706
- <p>You can also use nested <tt>assert_select</tt> blocks. In this case the inner <tt>assert_select</tt> runs the assertion on the complete collection of elements selected by the outer <tt>assert_select</tt> block:</p>
707
- <div class="code_container">
708
- <pre class="brush: ruby; gutter: false; toolbar: false">
709
- assert_select 'ul.navigation' do
710
- assert_select 'li.menu_item'
711
- end
712
- </pre>
713
- </div>
714
- <p>Alternatively the collection of elements selected by the outer <tt>assert_select</tt> may be iterated through so that <tt>assert_select</tt> 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>
715
- <div class="code_container">
716
- <pre class="brush: ruby; gutter: false; toolbar: false">
717
- assert_select &quot;ol&quot; do |elements|
718
- elements.each do |element|
719
- assert_select element, &quot;li&quot;, 4
720
- end
721
- end
722
-
723
- assert_select &quot;ol&quot; do
724
- assert_select &quot;li&quot;, 8
725
- end
726
- </pre>
727
- </div>
728
- <p>The <tt>assert_select</tt> assertion is quite powerful. For more advanced usage, refer to its <a href="http://api.rubyonrails.org/classes/ActionDispatch/Assertions/SelectorAssertions.html">documentation</a>.</p>
729
- <h5 id="additional-view-based-assertions">4.6.1 Additional View-Based Assertions</h5>
730
- <p>There are more assertions that are primarily used in testing views:</p>
731
- <table>
732
- <tr>
733
- <th>Assertion </th>
734
- <th>Purpose</th>
735
- </tr>
736
- <tr>
737
- <td><tt>assert_select_email</tt> </td>
738
- <td>Allows you to make assertions on the body of an e-mail. </td>
739
- </tr>
740
- <tr>
741
- <td><tt>assert_select_encoded</tt> </td>
742
- <td>Allows you to make assertions on encoded <span class="caps">HTML</span>. It does this by un-encoding the contents of each element and then calling the block with all the un-encoded elements.</td>
743
- </tr>
744
- <tr>
745
- <td><tt>css_select(selector)</tt> or <tt>css_select(element, selector)</tt> </td>
746
- <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>
747
- </tr>
748
- </table>
749
- <p>Here&#8217;s an example of using <tt>assert_select_email</tt>:</p>
750
- <div class="code_container">
751
- <pre class="brush: ruby; gutter: false; toolbar: false">
752
- assert_select_email do
753
- assert_select 'small', 'Please click the &quot;Unsubscribe&quot; link if you want to opt-out.'
754
- end
755
- </pre>
756
- </div>
757
- <h3 id="integration-testing">5 Integration Testing</h3>
758
- <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>
759
- <p>Unlike Unit and Functional tests, integration tests have to be explicitly created under the &#8216;test/integration&#8217; folder within your application. Rails provides a generator to create an integration test skeleton for you.</p>
760
- <div class="code_container">
761
- <pre class="brush: plain; gutter: false; toolbar: false">
762
- $ rails generate integration_test user_flows
763
- exists test/integration/
764
- create test/integration/user_flows_test.rb
765
- </pre>
766
- </div>
767
- <p>Here&#8217;s what a freshly-generated integration test looks like:</p>
768
- <div class="code_container">
769
- <pre class="brush: ruby; gutter: false; toolbar: false">
770
- require 'test_helper'
771
-
772
- class UserFlowsTest &lt; ActionDispatch::IntegrationTest
773
- fixtures :all
774
-
775
- # Replace this with your real tests.
776
- test &quot;the truth&quot; do
777
- assert true
778
- end
779
- end
780
- </pre>
781
- </div>
782
- <p>Integration tests inherit from <tt>ActionDispatch::IntegrationTest</tt>. 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>
783
- <h4 id="helpers-available-for-integration-tests">5.1 Helpers Available for Integration Tests</h4>
784
- <p>In addition to the standard testing helpers, there are some additional helpers available to integration tests:</p>
785
- <table>
786
- <tr>
787
- <th>Helper </th>
788
- <th>Purpose</th>
789
- </tr>
790
- <tr>
791
- <td><tt>https?</tt> </td>
792
- <td>Returns <tt>true</tt> if the session is mimicking a secure <span class="caps">HTTPS</span> request.</td>
793
- </tr>
794
- <tr>
795
- <td><tt>https!</tt> </td>
796
- <td>Allows you to mimic a secure <span class="caps">HTTPS</span> request.</td>
797
- </tr>
798
- <tr>
799
- <td><tt>host!</tt> </td>
800
- <td>Allows you to set the host name to use in the next request.</td>
801
- </tr>
802
- <tr>
803
- <td><tt>redirect?</tt> </td>
804
- <td>Returns <tt>true</tt> if the last request was a redirect.</td>
805
- </tr>
806
- <tr>
807
- <td><tt>follow_redirect!</tt> </td>
808
- <td>Follows a single redirect response.</td>
809
- </tr>
810
- <tr>
811
- <td><tt>request_via_redirect(http_method, path, [parameters], [headers])</tt> </td>
812
- <td>Allows you to make an <span class="caps">HTTP</span> request and follow any subsequent redirects.</td>
813
- </tr>
814
- <tr>
815
- <td><tt>post_via_redirect(path, [parameters], [headers])</tt> </td>
816
- <td>Allows you to make an <span class="caps">HTTP</span> <span class="caps">POST</span> request and follow any subsequent redirects.</td>
817
- </tr>
818
- <tr>
819
- <td><tt>get_via_redirect(path, [parameters], [headers])</tt> </td>
820
- <td>Allows you to make an <span class="caps">HTTP</span> <span class="caps">GET</span> request and follow any subsequent redirects.</td>
821
- </tr>
822
- <tr>
823
- <td><tt>put_via_redirect(path, [parameters], [headers])</tt> </td>
824
- <td>Allows you to make an <span class="caps">HTTP</span> <span class="caps">PUT</span> request and follow any subsequent redirects.</td>
825
- </tr>
826
- <tr>
827
- <td><tt>delete_via_redirect(path, [parameters], [headers])</tt> </td>
828
- <td>Allows you to make an <span class="caps">HTTP</span> <span class="caps">DELETE</span> request and follow any subsequent redirects.</td>
829
- </tr>
830
- <tr>
831
- <td><tt>open_session</tt> </td>
832
- <td>Opens a new session instance.</td>
833
- </tr>
834
- </table>
835
- <h4 id="integration-testing-examples">5.2 Integration Testing Examples</h4>
836
- <p>A simple integration test that exercises multiple controllers:</p>
837
- <div class="code_container">
838
- <pre class="brush: ruby; gutter: false; toolbar: false">
839
- require 'test_helper'
840
-
841
- class UserFlowsTest &lt; ActionDispatch::IntegrationTest
842
- fixtures :users
843
-
844
- test &quot;login and browse site&quot; do
845
- # login via https
846
- https!
847
- get &quot;/login&quot;
848
- assert_response :success
849
-
850
- post_via_redirect &quot;/login&quot;, :username =&gt; users(:avs).username, :password =&gt; users(:avs).password
851
- assert_equal '/welcome', path
852
- assert_equal 'Welcome avs!', flash[:notice]
853
-
854
- https!(false)
855
- get &quot;/posts/all&quot;
856
- assert_response :success
857
- assert assigns(:products)
858
- end
859
- end
860
- </pre>
861
- </div>
862
- <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 <span class="caps">DSL</span> (domain-specific language) just for your application.</p>
863
- <p>Here&#8217;s an example of multiple sessions and custom <span class="caps">DSL</span> in an integration test</p>
864
- <div class="code_container">
865
- <pre class="brush: ruby; gutter: false; toolbar: false">
866
- require 'test_helper'
867
-
868
- class UserFlowsTest &lt; ActionDispatch::IntegrationTest
869
- fixtures :users
870
-
871
- test &quot;login and browse site&quot; do
872
-
873
- # User avs logs in
874
- avs = login(:avs)
875
- # User guest logs in
876
- guest = login(:guest)
877
-
878
- # Both are now available in different sessions
879
- assert_equal 'Welcome avs!', avs.flash[:notice]
880
- assert_equal 'Welcome guest!', guest.flash[:notice]
881
-
882
- # User avs can browse site
883
- avs.browses_site
884
- # User guest can browse site as well
885
- guest.browses_site
886
-
887
- # Continue with other assertions
888
- end
889
-
890
- private
891
-
892
- module CustomDsl
893
- def browses_site
894
- get &quot;/products/all&quot;
895
- assert_response :success
896
- assert assigns(:products)
897
- end
898
- end
899
-
900
- def login(user)
901
- open_session do |sess|
902
- sess.extend(CustomDsl)
903
- u = users(user)
904
- sess.https!
905
- sess.post &quot;/login&quot;, :username =&gt; u.username, :password =&gt; u.password
906
- assert_equal '/welcome', path
907
- sess.https!(false)
908
- end
909
- end
910
- end
911
- </pre>
912
- </div>
913
- <h3 id="rake-tasks-for-running-your-tests">6 Rake Tasks for Running your Tests</h3>
914
- <p>You don&#8217;t need to set up and run your tests by hand on a test-by-test basis. Rails comes with a number of rake tasks to help in testing. The table below lists all rake tasks that come along in the default Rakefile when you initiate a Rails project.</p>
915
- <table>
916
- <tr>
917
- <th>Tasks </th>
918
- <th>Description</th>
919
- </tr>
920
- <tr>
921
- <td><tt>rake test</tt> </td>
922
- <td>Runs all unit, functional and integration tests. You can also simply run <tt>rake</tt> as the <em>test</em> target is the default.</td>
923
- </tr>
924
- <tr>
925
- <td><tt>rake test:benchmark</tt> </td>
926
- <td>Benchmark the performance tests</td>
927
- </tr>
928
- <tr>
929
- <td><tt>rake test:functionals</tt> </td>
930
- <td>Runs all the functional tests from <tt>test/functional</tt></td>
931
- </tr>
932
- <tr>
933
- <td><tt>rake test:integration</tt> </td>
934
- <td>Runs all the integration tests from <tt>test/integration</tt></td>
935
- </tr>
936
- <tr>
937
- <td><tt>rake test:plugins</tt> </td>
938
- <td>Run all the plugin tests from <tt>vendor/plugins/*/**/test</tt> (or specify with <tt>PLUGIN=_name_</tt>)</td>
939
- </tr>
940
- <tr>
941
- <td><tt>rake test:profile</tt> </td>
942
- <td>Profile the performance tests</td>
943
- </tr>
944
- <tr>
945
- <td><tt>rake test:recent</tt> </td>
946
- <td>Tests recent changes</td>
947
- </tr>
948
- <tr>
949
- <td><tt>rake test:uncommitted</tt> </td>
950
- <td>Runs all the tests which are uncommitted. Supports Subversion and Git</td>
951
- </tr>
952
- <tr>
953
- <td><tt>rake test:units</tt> </td>
954
- <td>Runs all the unit tests from <tt>test/unit</tt></td>
955
- </tr>
956
- </table>
957
- <h3 id="brief-note-about-test-unit">7 Brief Note About <tt>Test::Unit</tt></h3>
958
- <p>Ruby ships with a boat load of libraries. One little gem of a library is <tt>Test::Unit</tt>, a framework for unit testing in Ruby. All the basic assertions discussed above are actually defined in <tt>Test::Unit::Assertions</tt>. The class <tt>ActiveSupport::TestCase</tt> which we have been using in our unit and functional tests extends <tt>Test::Unit::TestCase</tt>, allowing
959
- us to use all of the basic assertions in our tests.</p>
960
- <div class="note"><p>For more information on <tt>Test::Unit</tt>, refer to <a href="http://ruby-doc.org/stdlib/libdoc/test/unit/rdoc/">test/unit Documentation</a></p></div>
961
- <h3 id="setup-and-teardown">8 Setup and Teardown</h3>
962
- <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&#8217;s take note of this by looking at an example for our functional test in <tt>Posts</tt> controller:</p>
963
- <div class="code_container">
964
- <pre class="brush: ruby; gutter: false; toolbar: false">
965
- require 'test_helper'
966
-
967
- class PostsControllerTest &lt; ActionController::TestCase
968
-
969
- # called before every single test
970
- def setup
971
- @post = posts(:one)
972
- end
973
-
974
- # called after every single test
975
- def teardown
976
- # as we are re-initializing @post before every test
977
- # setting it to nil here is not essential but I hope
978
- # you understand how you can use the teardown method
979
- @post = nil
980
- end
981
-
982
- test &quot;should show post&quot; do
983
- get :show, :id =&gt; @post.id
984
- assert_response :success
985
- end
986
-
987
- test &quot;should destroy post&quot; do
988
- assert_difference('Post.count', -1) do
989
- delete :destroy, :id =&gt; @post.id
990
- end
991
-
992
- assert_redirected_to posts_path
993
- end
994
-
995
- end
996
- </pre>
997
- </div>
998
- <p>Above, the <tt>setup</tt> method is called before each test and so <tt>@post</tt> is available for each of the tests. Rails implements <tt>setup</tt> and <tt>teardown</tt> as <tt>ActiveSupport::Callbacks</tt>. Which essentially means you need not only use <tt>setup</tt> and <tt>teardown</tt> as methods in your tests. You could specify them by using:</p>
999
- <ul>
1000
- <li>a block</li>
1001
- <li>a method (like in the earlier example)</li>
1002
- <li>a method name as a symbol</li>
1003
- <li>a lambda</li>
1004
- </ul>
1005
- <p>Let&#8217;s see the earlier example by specifying <tt>setup</tt> callback by specifying a method name as a symbol:</p>
1006
- <div class="code_container">
1007
- <pre class="brush: ruby; gutter: false; toolbar: false">
1008
- require '../test_helper'
1009
-
1010
- class PostsControllerTest &lt; ActionController::TestCase
1011
-
1012
- # called before every single test
1013
- setup :initialize_post
1014
-
1015
- # called after every single test
1016
- def teardown
1017
- @post = nil
1018
- end
1019
-
1020
- test &quot;should show post&quot; do
1021
- get :show, :id =&gt; @post.id
1022
- assert_response :success
1023
- end
1024
-
1025
- test &quot;should update post&quot; do
1026
- put :update, :id =&gt; @post.id, :post =&gt; { }
1027
- assert_redirected_to post_path(assigns(:post))
1028
- end
1029
-
1030
- test &quot;should destroy post&quot; do
1031
- assert_difference('Post.count', -1) do
1032
- delete :destroy, :id =&gt; @post.id
1033
- end
1034
-
1035
- assert_redirected_to posts_path
1036
- end
1037
-
1038
- private
1039
-
1040
- def initialize_post
1041
- @post = posts(:one)
1042
- end
1043
-
1044
- end
1045
- </pre>
1046
- </div>
1047
- <h3 id="testing-routes">9 Testing Routes</h3>
1048
- <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 <tt>show</tt> action of <tt>Posts</tt> controller above should look like:</p>
1049
- <div class="code_container">
1050
- <pre class="brush: ruby; gutter: false; toolbar: false">
1051
- test &quot;should route to post&quot; do
1052
- assert_routing '/posts/1', { :controller =&gt; &quot;posts&quot;, :action =&gt; &quot;show&quot;, :id =&gt; &quot;1&quot; }
1053
- end
1054
- </pre>
1055
- </div>
1056
- <h3 id="testing-your-mailers">10 Testing Your Mailers</h3>
1057
- <p>Testing mailer classes requires some specific tools to do a thorough job.</p>
1058
- <h4 id="keeping-the-postman-in-check">10.1 Keeping the Postman in Check</h4>
1059
- <p>Your mailer classes &#8212; like every other part of your Rails application &#8212; should be tested to ensure that it is working as expected.</p>
1060
- <p>The goals of testing your mailer classes are to ensure that:</p>
1061
- <ul>
1062
- <li>emails are being processed (created and sent)</li>
1063
- <li>the email content is correct (subject, sender, body, etc)</li>
1064
- <li>the right emails are being sent at the right times</li>
1065
- </ul>
1066
- <h5 id="from-all-sides">10.1.1 From All Sides</h5>
1067
- <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&#8217;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>
1068
- <h4 id="unit-testing">10.2 Unit Testing</h4>
1069
- <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>
1070
- <h5 id="revenge-of-the-fixtures">10.2.1 Revenge of the Fixtures</h5>
1071
- <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 <tt>test/fixtures</tt> directly corresponds to the name of the mailer. So, for a mailer named <tt>UserMailer</tt>, the fixtures should reside in <tt>test/fixtures/user_mailer</tt> directory.</p>
1072
- <p>When you generated your mailer, the generator creates stub fixtures for each of the mailers actions. If you didn&#8217;t use the generator you&#8217;ll have to make those files yourself.</p>
1073
- <h5 id="the-basic-test-case">10.2.2 The Basic Test Case</h5>
1074
- <p>Here&#8217;s a unit test to test a mailer named <tt>UserMailer</tt> whose action <tt>invite</tt> is used to send an invitation to a friend. It is an adapted version of the base test created by the generator for an <tt>invite</tt> action.</p>
1075
- <div class="code_container">
1076
- <pre class="brush: ruby; gutter: false; toolbar: false">
1077
- require 'test_helper'
1078
-
1079
- class UserMailerTest &lt; ActionMailer::TestCase
1080
- tests UserMailer
1081
- test &quot;invite&quot; do
1082
- @expected.from = 'me@example.com'
1083
- @expected.to = 'friend@example.com'
1084
- @expected.subject = &quot;You have been invited by #{@expected.from}&quot;
1085
- @expected.body = read_fixture('invite')
1086
- @expected.date = Time.now
1087
-
1088
- assert_equal @expected.encoded, UserMailer.create_invite('me@example.com', 'friend@example.com', @expected.date).encoded
1089
- end
1090
-
1091
- end
1092
- </pre>
1093
- </div>
1094
- <p>In this test, <tt>@expected</tt> is an instance of <tt>TMail::Mail</tt> that you can use in your tests. It is defined in <tt>ActionMailer::TestCase</tt>. The test above uses <tt>@expected</tt> to construct an email, which it then asserts with email created by the custom mailer. The <tt>invite</tt> fixture is the body of the email and is used as the sample content to assert against. The helper <tt>read_fixture</tt> is used to read in the content from this file.</p>
1095
- <p>Here&#8217;s the content of the <tt>invite</tt> fixture:</p>
1096
- <pre>
1097
- Hi friend@example.com,
1098
-
1099
- You have been invited.
1100
-
1101
- Cheers!
1102
- </pre>
1103
- <p>This is the right time to understand a little more about writing tests for your mailers. The line <tt>ActionMailer::Base.delivery_method = :test</tt> in <tt>config/environments/test.rb</tt> sets the delivery method to test mode so that email will not actually be delivered (useful to avoid spamming your users while testing) but instead it will be appended to an array (<tt>ActionMailer::Base.deliveries</tt>).</p>
1104
- <p>However often in unit tests, mails will not actually be sent, simply constructed, as in the example above, where the precise content of the email is checked against what it should be.</p>
1105
- <h4 id="functional-testing">10.3 Functional Testing</h4>
1106
- <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>
1107
- <div class="code_container">
1108
- <pre class="brush: ruby; gutter: false; toolbar: false">
1109
- require 'test_helper'
1110
-
1111
- class UserControllerTest &lt; ActionController::TestCase
1112
- test &quot;invite friend&quot; do
1113
- assert_difference 'ActionMailer::Base.deliveries.size', +1 do
1114
- post :invite_friend, :email =&gt; 'friend@example.com'
1115
- end
1116
- invite_email = ActionMailer::Base.deliveries.first
1117
-
1118
- assert_equal &quot;You have been invited by me@example.com&quot;, invite_email.subject
1119
- assert_equal 'friend@example.com', invite_email.to[0]
1120
- assert_match(/Hi friend@example.com/, invite_email.body)
1121
- end
1122
- end
1123
- </pre>
1124
- </div>
1125
- <h3 id="other-testing-approaches">11 Other Testing Approaches</h3>
1126
- <p>The built-in <tt>test/unit</tt> 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>
1127
- <ul>
1128
- <li><a href="http://avdi.org/projects/nulldb/">NullDB</a>, a way to speed up testing by avoiding database use.</li>
1129
- <li><a href="https://github.com/thoughtbot/factory_girl/tree/master">Factory Girl</a>, a replacement for fixtures.</li>
1130
- <li><a href="https://github.com/notahat/machinist/tree/master">Machinist</a>, another replacement for fixtures.</li>
1131
- <li><a href="http://www.thoughtbot.com/projects/shoulda">Shoulda</a>, an extension to <tt>test/unit</tt> with additional helpers, macros, and assertions.</li>
1132
- <li><a href="http://relishapp.com/rspec">RSpec</a>, a behavior-driven development framework</li>
1133
- </ul>
1134
-
1135
- <h3>Feedback</h3>
1136
- <p>
1137
- You're encouraged to help improve the quality of this guide.
1138
- </p>
1139
- <p>
1140
- If you see any typos or factual errors you are confident to
1141
- patch, please clone <a href="https://github.com/lifo/docrails">docrails</a>
1142
- and push the change yourself. That branch of Rails has public write access.
1143
- Commits are still reviewed, but that happens after you've submitted your
1144
- contribution. <a href="https://github.com/lifo/docrails">docrails</a> is
1145
- cross-merged with master periodically.
1146
- </p>
1147
- <p>
1148
- You may also find incomplete content, or stuff that is not up to date.
1149
- Please do add any missing documentation for master. Check the
1150
- <a href="ruby_on_rails_guides_guidelines.html">Ruby on Rails Guides Guidelines</a>
1151
- for style and conventions.
1152
- </p>
1153
- <p>
1154
- If for whatever reason you spot something to fix but cannot patch it yourself, please
1155
- <a href="https://github.com/rails/rails/issues">open an issue</a>.
1156
- </p>
1157
- <p>And last but not least, any kind of discussion regarding Ruby on Rails
1158
- documentation is very welcome in the <a href="http://groups.google.com/group/rubyonrails-docs">rubyonrails-docs mailing list</a>.
1159
- </p>
1160
- </div>
1161
- </div>
1162
- </div>
1163
-
1164
- <hr class="hide" />
1165
- <div id="footer">
1166
- <div class="wrapper">
1167
- <p>This work is licensed under a <a href="http://creativecommons.org/licenses/by-sa/3.0/">Creative Commons Attribution-Share Alike 3.0</a> License</p>
1168
- <p>"Rails", "Ruby on Rails", and the Rails logo are trademarks of David Heinemeier Hansson. All rights reserved.</p>
1169
- </div>
1170
- </div>
1171
-
1172
- <script type="text/javascript" src="javascripts/guides.js"></script>
1173
- <script type="text/javascript" src="javascripts/syntaxhighlighter/shCore.js"></script>
1174
- <script type="text/javascript" src="javascripts/syntaxhighlighter/shBrushRuby.js"></script>
1175
- <script type="text/javascript" src="javascripts/syntaxhighlighter/shBrushXml.js"></script>
1176
- <script type="text/javascript" src="javascripts/syntaxhighlighter/shBrushSql.js"></script>
1177
- <script type="text/javascript" src="javascripts/syntaxhighlighter/shBrushPlain.js"></script>
1178
- <script type="text/javascript">
1179
- SyntaxHighlighter.all()
1180
- </script>
1181
- </body>
1182
- </html>