Scala中的TDD Getters和Setter

时间:2014-08-10 16:06:05

标签: scala tdd getter-setter

测试

package com.utrecht.numbersequences

import org.scalatest.FunSuite
import org.scalatest.BeforeAndAfter
import org.scalatest.mock.MockitoSugar
import org.mockito.Mockito._

class NumberSequencesTests extends FunSuite with BeforeAndAfter with MockitoSugar {
  test("testCity") {
    NumberSequences.city_("utrecht")
    assert("utrecht" === NumberSequences.city())
  }
}

代码

package com.utrecht.numbersequences

import scala.collection.immutable.Stream.consWrapper

object NumberSequences {
  var _city: String = null 

  def city_=(_city:String) = this._city = _city  
  def city = this._city
}

结果

  • 值不是对象的成员
  • 没有足够的方法适用于方法:(索引:Int)类StringOps中的Char

      

    测试   [info]将1个Scala源编译为C:\ path \ to \ developme   NT \ scalaNumberSequences \目标\斯卡拉2.10 \测试类...   [错误] C:\ path \ to \ development \ scalaNumberSequences \ src   \ test \ scala \ com \ utrecht \ numbersequences \ NumberSequencesTest.scala:32:value city   _不是对象com.utrecht.numbersequences.NumberSequences的成员   [错误] NumberSequences.city _(" utrecht")   [错误] ^   [错误] C:\ path \ to \ development \ scalaNumberSequences \ src   \ test \ scala \ com \ utrecht \ numbersequences \ NumberSequencesTest.scala:33:还不够    方法apply的参数:(index:Int)类StringOps中的Char。   [错误]未指定的值参数索引。   [错误]断言(" utrecht" === NumberSequences.city())   [错误] ^   [错误]发现两个错误   [错误](test:compile)编译失败   [错误]总时间:1秒,2014年8月10日下午5:52:16

1 个答案:

答案 0 :(得分:3)

NumberSequences.city_=("utrecht")
//OR
NumberSequences.city = "utrecht"

但不是:

NumberSequences.city_("utrecht") // city_ is not a method existing in the object