我可以反复使用不同的值对表单进行spock测试吗?

时间:2012-03-05 16:21:11

标签: grails spock geb

我是Spock和Geb的新手,并且正在我的Grails 1.3.7应用程序中使用它们。我有一个LoginSpec和LoginPage工作。现在我想要做的是多次执行LoginSpec,指定几个不同的用户名/密码组合。我不清楚我应该如何解决这个问题。

class LoginSpec extends geb.spock.GebReportingSpec {

  def "At the login page"() {
    when: "we visit the login page"
    to LoginPage
    then:
    at LoginPage
  }

  def "failed login"() {
    when: "a user goes to the login page and enters invalid data"
    to LoginPage
    loginForm.j_username = "bad@test.com"
    loginForm.j_password = "password"
    loginButton.click()
    then:
    at LoginPage
  }

  def "successful login"() {
    when: "a user goes to the login page and enters valid data"
    to LoginPage
    loginForm.j_username = "good@test.com"
    loginForm.j_password = "password"
    loginButton.click()
    then:
    at DashboardPage
  }
}

class LoginPage extends Page {

  static url = "login/auth"

  static at = { heading == "sign in" }

  static content = {
    heading { $(".page-header>h2").text() }
    loginButton(to: [DashboardPage, LoginPage]) { $('#sign_in').find("button.primary") }
    loginForm { $('#loginForm') }

  }
}

1 个答案:

答案 0 :(得分:3)

您可以使用Spock的支持进行数据驱动测试:

def "successful login"() {
    when: "a user goes to the login page and enters valid data"
    to LoginPage
    loginForm.j_username = username
    loginForm.j_password = password
    loginButton.click()

    then:
    at DashboardPage

    where:
    username        | password
    "test@test.com" | "password"
    "foo"           | "bar"        
}