简体   繁体   中英

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 selenium : 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM