tags:

views:

116

answers:

1

At the moment I am playing with Griffon. Everything works very smooth except for the testing.

I like to test the separate controller methods without starting the whole Griffon application. To do this it seems to me, that I have to mock the view and the model which are used in the controller. Because of the mocking with Expando objects, the tests for the controller methods and actions with easyb are getting too long.

Here is a simple example:

MyProjectView.groovy

application(title: 'MyProject',
    pack: true,
    locationByPlatform: true,
    iconImage: imageIcon('/griffon-icon-48x48.png').image,
    iconImages: [imageIcon('/griffon-icon-48x48.png').image,
            imageIcon('/griffon-icon-32x32.png').image,
            imageIcon('/griffon-icon-16x16.png').image]
) {
tableLayout {
tr {
  td(align: "CENTER") {
    textField(id: 'textfield',
            text: "Hello")
  }
}

tr {
  td(align: "CENTER") {
    button(text: "check",
            actionPerformed: controller.checkForGreeting
    )
   }
  }
 }
}

MyProjectController.groovy

class MyProjectController {
def model
def view

void mvcGroupInit(Map args) {
}

def checkForGreeting = { evt = null ->
  return view.textfield.text == "Hello"
}

MyProjectModel.groovy

class MyProjectModel {}

the easyb test: MyProjectStory.story

scenario "Hello Check", {
  def view
  MyProjectController controller = new MyProjectController()

given "A view with 'Hello' in the textfield", {
  view = new Expando()
  def textfield = new Expando()
  textfield.text = "Hello"
  view.textfield = textfield
  controller.view = view
}
then "checkForGreeting should return true", {
  controller.checkForGreeting().shouldBe(true)
 }
}

Is there a simpler way to test Griffon controller methods? Perhaps by using a better solutions for mocking the view?

+1  A: 

There is no mocking plugin/facilities in Griffon like the ones you can find in Grails at the moment. Testing controllers is usually done in integration tests (that is why the create-mvc template places a test under test/integration).

However mocking facilities are not out of the question for a future release.

aalmiray