【问题标题】:Too many arguments for constructor构造函数的参数太多
【发布时间】:2017-04-04 07:31:24
【问题描述】:

尝试学习一些 Scala。

我的项目中有以下类:

package com.fluentaws

class AwsProvider(val accountId: String, val accountSecret: String) {

 def AwsAccount = new AwsAccount(accountId, accountSecret)

}

class AwsAccount(val accountId : String, val accountSecret : String) {

}

还有以下测试:

package com.fluentaws

import org.scalatest._

class AwsProvider extends FunSuite {

  test("When providing AwsProvider with AWS Credentials we can retrieve an AwsAccount with the same values") {

    val awsAccountId = "abc"
    val awsAccountSecret = "secret"

    val awsProvider = new AwsProvider(awsAccountId, awsAccountSecret)

    val awsAccount = awsProvider.AwsAccount

    assert(awsAccount.accountId == awsAccountId)
    assert(awsAccount.accountSecret == awsAccountSecret)
  }

}

当我的测试套件运行时,我收到编译时错误:

构造函数 AwsProvider 的参数过多: ()com.fluentaws.AwsProvider [错误] val awsProvider = new AwsProvider(awsAccountId, awsAccountSecret) [错误]

从错误消息看来,它看到了一个零参数的构造函数?

谁能看到我在这里做错了什么?

【问题讨论】:

  • 哦,.. 也许我正在重新定义一个名为 AwsProvider 的新类,而不是扩展现有的类
  • 你应该重命名你的测试类。
  • 是的,就是这样:-)

标签: scala constructor tdd classname


【解决方案1】:

这是一个典型的菜鸟错误。我修复了我的测试类的名称,因为使用相同的名称会影响原始名称,因此我实际上是在测试我的测试类:

package com.fluentaws

import org.scalatest._

class AwsProviderTestSuite extends FunSuite {

  test("When providing AwsProvider with AWS Credentials we can retrieve an AwsAccount with the same values") {

    val awsAccountId = "abc"
    val awsAccountSecret = "secret"

    val awsProvider = new AwsProvider(awsAccountId, awsAccountSecret)

    val awsAccount = awsProvider.AwsAccount

    assert(awsAccount.accountId == awsAccountId)
    assert(awsAccount.accountSecret == awsAccountSecret)
  }

}

现在它通过了。

【讨论】:

    猜你喜欢
    • 2011-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-14
    • 1970-01-01
    • 2016-02-28
    相关资源
    最近更新 更多