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?

4

1 回答 1

1

Griffon 中没有像您目前在 Grails 中可以找到的那样的模拟插件/工具。测试控制器通常在集成测试中完成(这就是 create-mvc 模板将测试置于 test/integration 之下的原因)。

然而,对于未来的版本来说,模拟设施并不是不可能的。

于 2009-08-28T06:09:28.140 回答