You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

around_hooks.feature 7.0 kB

2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. Feature: Around hooks
  2. In order to support transactional scenarios for database libraries
  3. that provide only a block syntax for transactions, Cucumber should
  4. permit definition of Around hooks.
  5. Background:
  6. Given a file named "features/support/env.rb" with:
  7. """
  8. module HookLoggerWorld
  9. def logged_hooks
  10. @logged_hooks ||= []
  11. end
  12. def log_hook(name)
  13. logged_hooks << name
  14. end
  15. end
  16. World(HookLoggerWorld)
  17. """
  18. Scenario: A single Around hook
  19. Given a file named "features/step_definitions/steps.rb" with:
  20. """
  21. Then /^the hook is called$/ do
  22. expect(logged_hooks).to eq(['Around'])
  23. end
  24. """
  25. And a file named "features/support/hooks.rb" with:
  26. """
  27. Around do |scenario, block|
  28. log_hook('Around')
  29. block.call
  30. end
  31. """
  32. And a file named "features/f.feature" with:
  33. """
  34. Feature: Around hooks
  35. Scenario: using hook
  36. Then the hook is called
  37. """
  38. When I run `cucumber features/f.feature`
  39. Then it should pass with:
  40. """
  41. Feature: Around hooks
  42. Scenario: using hook # features/f.feature:2
  43. Then the hook is called # features/step_definitions/steps.rb:1
  44. 1 scenario (1 passed)
  45. 1 step (1 passed)
  46. """
  47. Scenario: Multiple Around hooks
  48. Given a file named "features/step_definitions/steps.rb" with:
  49. """
  50. Then /^the hooks are called in the correct order$/ do
  51. expect(logged_hooks).to eq(['A', 'B', 'C'])
  52. end
  53. """
  54. And a file named "features/support/hooks.rb" with:
  55. """
  56. Around do |scenario, block|
  57. log_hook('A')
  58. block.call
  59. end
  60. Around do |scenario, block|
  61. log_hook('B')
  62. block.call
  63. end
  64. Around do |scenario, block|
  65. log_hook('C')
  66. block.call
  67. end
  68. """
  69. And a file named "features/f.feature" with:
  70. """
  71. Feature: Around hooks
  72. Scenario: using multiple hooks
  73. Then the hooks are called in the correct order
  74. """
  75. When I run `cucumber features/f.feature`
  76. Then it should pass with:
  77. """
  78. Feature: Around hooks
  79. Scenario: using multiple hooks # features/f.feature:2
  80. Then the hooks are called in the correct order # features/step_definitions/steps.rb:1
  81. 1 scenario (1 passed)
  82. 1 step (1 passed)
  83. """
  84. Scenario: Mixing Around, Before, and After hooks
  85. Given a file named "features/step_definitions/steps.rb" with:
  86. """
  87. Then /^the Around hook is called around Before and After hooks$/ do
  88. expect(logged_hooks).to eq(['Around', 'Before'])
  89. end
  90. """
  91. And a file named "features/support/hooks.rb" with:
  92. """
  93. Around do |scenario, block|
  94. log_hook 'Around'
  95. block.call
  96. log_hook 'Around'
  97. expect(logged_hooks).to eq(['Around', 'Before', 'After', 'Around'])
  98. end
  99. Before do |scenario|
  100. log_hook 'Before'
  101. end
  102. After do |scenario|
  103. log_hook 'After'
  104. expect(logged_hooks).to eq(['Around', 'Before', 'After'])
  105. end
  106. """
  107. And a file named "features/f.feature" with:
  108. """
  109. Feature: Around hooks
  110. Scenario: Mixing Around, Before, and After hooks
  111. Then the Around hook is called around Before and After hooks
  112. """
  113. When I run `cucumber features/f.feature`
  114. Then it should pass with:
  115. """
  116. Feature: Around hooks
  117. Scenario: Mixing Around, Before, and After hooks # features/f.feature:2
  118. Then the Around hook is called around Before and After hooks # features/step_definitions/steps.rb:1
  119. 1 scenario (1 passed)
  120. 1 step (1 passed)
  121. """
  122. Scenario: Around hooks with tags
  123. Given a file named "features/step_definitions/steps.rb" with:
  124. """
  125. Then /^the Around hooks with matching tags are called$/ do
  126. expect(logged_hooks).to eq(['one', 'one or two'])
  127. end
  128. """
  129. And a file named "features/support/hooks.rb" with:
  130. """
  131. Around('@one') do |scenario, block|
  132. log_hook('one')
  133. block.call
  134. end
  135. Around('@one or @two') do |scenario, block|
  136. log_hook('one or two')
  137. block.call
  138. end
  139. Around('@one and @two') do |scenario, block|
  140. log_hook('one and two')
  141. block.call
  142. end
  143. Around('@two') do |scenario, block|
  144. log_hook('two')
  145. block.call
  146. end
  147. """
  148. And a file named "features/f.feature" with:
  149. """
  150. Feature: Around hooks
  151. @one
  152. Scenario: Around hooks with tags
  153. Then the Around hooks with matching tags are called
  154. """
  155. When I run `cucumber -q -t @one features/f.feature`
  156. Then it should pass with:
  157. """
  158. Feature: Around hooks
  159. @one
  160. Scenario: Around hooks with tags
  161. Then the Around hooks with matching tags are called
  162. 1 scenario (1 passed)
  163. 1 step (1 passed)
  164. """
  165. Scenario: Around hooks with scenario outlines
  166. Given a file named "features/step_definitions/steps.rb" with:
  167. """
  168. Then /^the hook is called$/ do
  169. expect(logged_hooks).to eq(['Around'])
  170. end
  171. """
  172. And a file named "features/support/hooks.rb" with:
  173. """
  174. Around do |scenario, block|
  175. log_hook('Around')
  176. block.call
  177. end
  178. """
  179. And a file named "features/f.feature" with:
  180. """
  181. Feature: Around hooks with scenario outlines
  182. Scenario Outline: using hook
  183. Then the hook is called
  184. Examples:
  185. | Number |
  186. | one |
  187. | two |
  188. """
  189. When I run `cucumber features/f.feature`
  190. Then it should pass with:
  191. """
  192. Feature: Around hooks with scenario outlines
  193. Scenario Outline: using hook # features/f.feature:2
  194. Then the hook is called # features/f.feature:3
  195. Examples:
  196. | Number |
  197. | one |
  198. | two |
  199. 2 scenarios (2 passed)
  200. 2 steps (2 passed)
  201. """
  202. Scenario: Around Hooks and the Custom World
  203. Given a file named "features/step_definitions/steps.rb" with:
  204. """
  205. Then /^the world should be available in the hook$/ do
  206. $previous_world = self
  207. expect($hook_world).to eq(self)
  208. end
  209. Then /^what$/ do
  210. expect($hook_world).not_to eq($previous_world)
  211. end
  212. """
  213. And a file named "features/support/hooks.rb" with:
  214. """
  215. Around do |scenario, block|
  216. $hook_world = self
  217. block.call
  218. end
  219. """
  220. And a file named "features/f.feature" with:
  221. """
  222. Feature: Around hooks
  223. Scenario: using hook
  224. Then the world should be available in the hook
  225. Scenario: using the same hook
  226. Then what
  227. """
  228. When I run `cucumber features/f.feature`
  229. Then it should pass

No Description

Contributors (1)