Permalink
Browse files

Improve ScaffoldingSpec

ScaffoldingSpec now generates a controller and its corresponding unit test and then verifies that the test fails.  The test is supposed to fail until the TODOs in the generated test have been accounted for.
  • Loading branch information...
jeffbrown committed Dec 1, 2011
1 parent 6951d77 commit 8ca53d2bbba58430334ecf8a52c0a10613ea98ff
@@ -1,11 +1,16 @@
import spock.lang.*
class ScaffoldingSpec extends BaseSpec {
- void "failing before class methods should be handled"() {
+ void 'scaffolded controller test fails before TODOs are accounted for'() {
+ when:
+ execute 'scaffolding', 'generate-controller', 'scaffolding.Item'
+ then:
+ exitStatus == 0
+
when:
- execute('scaffolding', 'test-app', ':unit')
+ execute 'scaffolding', 'test-app', ':unit'
then:
- exitStatus == 1
- output.contains("Completed 8 unit tests, 5 failed")
+ exitStatus != 0
+ output.contains "Completed 8 unit tests, 5 failed"
}
}
@@ -1,102 +0,0 @@
-package scaffolding
-
-import grails.converters.JSON
-
-class ItemController {
-
- static allowedMethods = [save: "POST", update: "POST", delete: "POST"]
-
- def index = {
- redirect(action: "list", params: params)
- }
-
- def list = {
- params.max = Math.min(params.max ? params.int('max') : 10, 100)
- [itemInstanceList: Item.list(params), itemInstanceTotal: Item.count()]
- }
-
- def create = {
- def itemInstance = new Item()
- itemInstance.properties = params
- return [itemInstance: itemInstance]
- }
-
- def save = {
- def itemInstance = new Item(params)
- if (itemInstance.save(flush: true)) {
- flash.message = message(code: 'default.created.message', args: [message(code: 'item.label', default: 'Item'), itemInstance.id])
- redirect(action: "show", id: itemInstance.id)
- }
- else {
- render(view: "create", model: [itemInstance: itemInstance])
- }
- }
-
- def show = {
- def itemInstance = Item.get(params.id)
- if (!itemInstance) {
- flash.message = message(code: 'default.not.found.message', args: [message(code: 'item.label', default: 'Item'), params.id])
- redirect(action: "list")
- }
- else {
- [itemInstance: itemInstance]
- }
- }
-
- def edit = {
- def itemInstance = Item.get(params.id)
- if (!itemInstance) {
- flash.message = message(code: 'default.not.found.message', args: [message(code: 'item.label', default: 'Item'), params.id])
- redirect(action: "list")
- }
- else {
- return [itemInstance: itemInstance]
- }
- }
-
- def update = {
- def itemInstance = Item.get(params.id)
- if (itemInstance) {
- if (params.version) {
- def version = params.version.toLong()
- if (itemInstance.version > version) {
-
- itemInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'item.label', default: 'Item')] as Object[], "Another user has updated this Item while you were editing")
- render(view: "edit", model: [itemInstance: itemInstance])
- return
- }
- }
- itemInstance.properties = params
- if (!itemInstance.hasErrors() && itemInstance.save(flush: true)) {
- flash.message = message(code: 'default.updated.message', args: [message(code: 'item.label', default: 'Item'), itemInstance.id])
- redirect(action: "show", id: itemInstance.id)
- }
- else {
- render(view: "edit", model: [itemInstance: itemInstance])
- }
- }
- else {
- flash.message = message(code: 'default.not.found.message', args: [message(code: 'item.label', default: 'Item'), params.id])
- redirect(action: "list")
- }
- }
-
- def delete = {
- def itemInstance = Item.get(params.id)
- if (itemInstance) {
- try {
- itemInstance.delete(flush: true)
- flash.message = message(code: 'default.deleted.message', args: [message(code: 'item.label', default: 'Item'), params.id])
- redirect(action: "list")
- }
- catch (org.springframework.dao.DataIntegrityViolationException e) {
- flash.message = message(code: 'default.not.deleted.message', args: [message(code: 'item.label', default: 'Item'), params.id])
- redirect(action: "show", id: params.id)
- }
- }
- else {
- flash.message = message(code: 'default.not.found.message', args: [message(code: 'item.label', default: 'Item'), params.id])
- redirect(action: "list")
- }
- }
-}
@@ -1,159 +0,0 @@
-package scaffolding
-
-
-
-import org.junit.*
-import grails.test.mixin.*
-
-@TestFor(ItemController)
-@Mock(Item)
-class ItemControllerTests {
-
-
- def populateValidParams(params) {
- assert params != null
- // TODO: Populate valid properties like...
- //params["name"] = 'someValidName'
- }
-
- void testIndex() {
- controller.index()
- assert "/item/list" == response.redirectedUrl
- }
-
- void testList() {
-
- def model = controller.list()
-
- assert model.itemInstanceList.size() == 0
- assert model.itemInstanceTotal == 0
- }
-
- void testCreate() {
- def model = controller.create()
-
- assert model.itemInstance != null
- }
-
- void testSave() {
- controller.save()
-
- assert model.itemInstance != null
- assert view == '/item/create'
-
- response.reset()
-
- populateValidParams(params)
- controller.save()
-
- assert response.redirectedUrl == '/item/show/1'
- assert controller.flash.message != null
- assert Item.count() == 1
- }
-
- void testShow() {
- controller.show()
-
- assert flash.message != null
- assert response.redirectedUrl == '/item/list'
-
-
- populateValidParams(params)
- def item = new Item(params)
-
- assert item.save() != null
-
- params.id = item.id
-
- def model = controller.show()
-
- assert model.itemInstance == item
- }
-
- void testEdit() {
- controller.edit()
-
- assert flash.message != null
- assert response.redirectedUrl == '/item/list'
-
-
- populateValidParams(params)
- def item = new Item(params)
-
- assert item.save() != null
-
- params.id = item.id
-
- def model = controller.edit()
-
- assert model.itemInstance == item
- }
-
- void testUpdate() {
- controller.update()
-
- assert flash.message != null
- assert response.redirectedUrl == '/item/list'
-
- response.reset()
-
-
- populateValidParams(params)
- def item = new Item(params)
-
- assert item.save() != null
-
- // test invalid parameters in update
- params.id = item.id
- //TODO: add invalid values to params object
-
- controller.update()
-
- assert view == "/item/edit"
- assert model.itemInstance != null
-
- item.clearErrors()
-
- populateValidParams(params)
- controller.update()
-
- assert response.redirectedUrl == "/item/show/$item.id"
- assert flash.message != null
-
- //test outdated version number
- response.reset()
- item.clearErrors()
-
- populateValidParams(params)
- params.id = item.id
- params.version = -1
- controller.update()
-
- assert view == "/item/edit"
- assert model.itemInstance != null
- assert model.itemInstance.errors.getFieldError('version')
- assert flash.message != null
- }
-
- void testDelete() {
- controller.delete()
- assert flash.message != null
- assert response.redirectedUrl == '/item/list'
-
- response.reset()
-
- populateValidParams(params)
- def item = new Item(params)
-
- assert item.save() != null
- assert Item.count() == 1
-
- params.id = item.id
-
- controller.delete()
-
- assert Item.count() == 0
- assert Item.get(item.id) == null
- assert response.redirectedUrl == '/item/list'
- }
-}

0 comments on commit 8ca53d2

Please sign in to comment.