消除小型自定义dsl中的括号以构建测试类

时间:2018-06-27 20:41:47

标签: scala dsl

我正在尝试使用小型DSL为UI测试获得更好的语法。为此,我定义了一个LoginRobotBuilder来构建我的LoginRobt(该类实际上进行了一些测试)和一些其他语法来使用LoginRobotBuilder

  sealed abstract class LoginRobotBuilder()(implicit val driver: WebDriver) {
    var user: String = ""
    var password: String = ""

    def user(user: String): LoginRobotBuilder = {
      this.user = user
      this
    }

    def password(password: String): LoginRobotBuilder = {
      this.password = password
      this
    }

    def build: LoginRobot = LoginRobot(user, password)
  }

  object LoginRobotSyntax {

    case class LoginRobotBuilderWrapper(loginRobotBuilder: LoginRobotBuilder)

    def login(builder: LoginRobotBuilder) : LoginResult = {
      builder.build.login()
    }

    def using()(implicit driver: WebDriver) = new LoginRobotBuilder {}

    def user(user: String)(builder: LoginRobotBuilder) = builder.user(user)

    def password(password: String)(builder: LoginRobotBuilder) = builder.password(password)
  }

我目前可以做的是这样的

test("Using builder - Login with user 'invalidUser' and password 'invalidPassword' shouldFail") {
    login( using user "invalidUser" password "invalidPassword" ) shouldFail
  }

此测试等同于

test("Login with user 'invalidUser' and password 'invalidPassword' shouldFail") {
    LoginRobot(user = "invalidUser", password = "invalidPassword").login.shouldFail
  }

有什么方法可以消除login之后和shouldFail之前的括号吗?

0 个答案:

没有答案