Why scala doesn't recognize method from trait
First of all i have trait:
import _root_.com.thoughtworks.selenium._
import org.scalatest.matchers.ShouldMatchers
import org.scalatest.matchers.ShouldMatchers._
trait SeleniumField extends ShouldMatchers {
val name : String
def sel开发者_高级运维enium : Selenium
def text : String = { return selenium.getValue(name) }
def is(v:String) : Boolean = { this.value equals v }
def set(v:String) = { selenium.`type`( name , v ) }
}
Then i create scala class with this trait:
import _root_.com.thoughtworks.selenium._
class WebAppField(sel:Selenium, nam: String) extends SeleniumField {
def selenium = sel
override val name = nam
}
And then when i try to use it in code:
val rodzaj = new WebAppField(selenium, "RODZAJ")
rodzaj text should equal "K"
i got:
error: not found: value should
[INFO] rodzaj text should equal "K"
What i do wrong?
Scala ver 2.8
When you omit dots and parentheses from method calls in Scala, they are always parsed the same way, assuming infix notation and single arguments.
rodzaj text should equal "K"
is the same as
rodzaj.text(should).equal("K")
Try rewriting it as:
rodzaj.text should equal("K")
or you could fully punctuate as:
rodzaj.text.should(equal("K"))
String doesn't normally have a should
method. ScalaTest makes it available via an implicit conversion.
In the place where you're writing your test, you need this import:
import org.scalatest.matchers.ShouldMatchers._
to bring that implicit into scope. It isn't enough for the import to appear in the code being tested.
It's kind of strange to have any references to ScalaTest at all in the code being tested, actually. Normally references to your test framework should only appear in your tests.
精彩评论