Advertisement
Guest User

Untitled

a guest
Nov 15th, 2018
254
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.83 KB | None | 0 0
  1. @TestFor(LagerortController)
  2. @Mock(Lagerort)
  3. class LagerortControllerSpec extends Specification {
  4.  
  5. def populateValidParams(params) {
  6. assert params != null
  7. params["lagerort"] = 'Fa.Conrad'
  8. params["lagertyp"] = ["lagertyp": 'Fa.Conrad', "beschreibung": 'Motor befindet sich bei Fa.Conrad']
  9. params["beschreibung"] = 'in Reparatur bei Fa. Conrad'
  10. }
  11.  
  12. void "Test the index action returns the correct model"() {
  13.  
  14. when:"The index action is executed"
  15. controller.index()
  16.  
  17. then:"The model is correct"
  18. !model.lagerortList
  19. model.lagerortCount == 0
  20. }
  21.  
  22. void "Test the create action returns the correct model"() {
  23. when:"The create action is executed"
  24. controller.create()
  25.  
  26. then:"The model is correctly created"
  27. model.lagerort!= null
  28. }
  29.  
  30. void "Test the save action correctly persists an instance"() {
  31.  
  32. when:"The save action is executed with an invalid instance"
  33. request.contentType = FORM_CONTENT_TYPE
  34. request.method = 'POST'
  35. def lagerort = new Lagerort()
  36. lagerort.validate()
  37. controller.save(lagerort)
  38.  
  39. then:"The create view is rendered again with the correct model"
  40. model.lagerort!= null
  41. view == 'create'
  42.  
  43. when:"The save action is executed with a valid instance"
  44. response.reset()
  45. populateValidParams(params)
  46. lagerort = new Lagerort(params)
  47.  
  48. controller.save(lagerort)
  49.  
  50. then:"A redirect is issued to the show action"
  51. response.redirectedUrl == '/lagerort/show/1'
  52. controller.flash.message != null
  53. Lagerort.count() == 1
  54. }
  55.  
  56. void "Test that the show action returns the correct model"() {
  57. when:"The show action is executed with a null domain"
  58. controller.show(null)
  59.  
  60. then:"A 404 error is returned"
  61. response.status == 404
  62.  
  63. when:"A domain instance is passed to the show action"
  64. populateValidParams(params)
  65. def lagerort = new Lagerort(params)
  66. controller.show(lagerort)
  67.  
  68. then:"A model is populated containing the domain instance"
  69. model.lagerort == lagerort
  70. }
  71.  
  72. void "Test that the edit action returns the correct model"() {
  73. when:"The edit action is executed with a null domain"
  74. controller.edit(null)
  75.  
  76. then:"A 404 error is returned"
  77. response.status == 404
  78.  
  79. when:"A domain instance is passed to the edit action"
  80. populateValidParams(params)
  81. def lagerort = new Lagerort(params)
  82. controller.edit(lagerort)
  83.  
  84. then:"A model is populated containing the domain instance"
  85. model.lagerort == lagerort
  86. }
  87.  
  88. void "Test the update action performs an update on a valid domain instance"() {
  89. when:"Update is called for a domain instance that doesn't exist"
  90. request.contentType = FORM_CONTENT_TYPE
  91. request.method = 'PUT'
  92. controller.update(null)
  93.  
  94. then:"A 404 error is returned"
  95. response.redirectedUrl == '/lagerort/index'
  96. flash.message != null
  97.  
  98. when:"An invalid domain instance is passed to the update action"
  99. response.reset()
  100. def lagerort = new Lagerort()
  101. lagerort.validate()
  102. controller.update(lagerort)
  103.  
  104. then:"The edit view is rendered again with the invalid instance"
  105. view == 'edit'
  106. model.lagerort == lagerort
  107.  
  108. when:"A valid domain instance is passed to the update action"
  109. response.reset()
  110. populateValidParams(params)
  111. lagerort = new Lagerort(params).save(flush: true)
  112. controller.update(lagerort)
  113.  
  114. then:"A redirect is issued to the show action"
  115. lagerort != null
  116. response.redirectedUrl == "/lagerort/show/$lagerort.id"
  117. flash.message != null
  118. }
  119.  
  120. void "Test that the delete action deletes an instance if it exists"() {
  121. when:"The delete action is called for a null instance"
  122. request.contentType = FORM_CONTENT_TYPE
  123. request.method = 'DELETE'
  124. controller.delete(null)
  125.  
  126. then:"A 404 is returned"
  127. response.redirectedUrl == '/lagerort/index'
  128. flash.message != null
  129.  
  130. when:"A domain instance is created"
  131. response.reset()
  132. populateValidParams(params)
  133. def lagerort = new Lagerort(params).save(flush: true)
  134.  
  135. then:"It exists"
  136. Lagerort.count() == 1
  137.  
  138. when:"The domain instance is passed to the delete action"
  139. controller.delete(lagerort)
  140.  
  141. then:"The instance is deleted"
  142. Lagerort.count() == 0
  143. response.redirectedUrl == '/lagerort/index'
  144. flash.message != null
  145. }
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement