2016-11-21 85 views
0

试图学习一些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) 
    } 

} 

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

too many arguments for constructor AwsProvider: ()com.fluentaws.AwsProvider [error] val awsProvider = new AwsProvider(awsAccountId, awsAccountSecret) [error]

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

任何人都可以看到我在做什么错在这里?

+0

哦,..也许我正在重新定义一个名为AwsProvider的新类,而不是扩展现有的类 – CodeMonkey

+2

您应该重命名您的测试类。 – tkausl

+0

是的,就是这样:-) – CodeMonkey

回答

2

这是一个典型的新秀错误。我修好了我的测试类的名字,因为使用相同的名称将影子原来的名字,因此我实际测试我的测试的类:

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) 
    } 

} 

现在它传递。