为什么 scala 无法识别特征中的方法
首先我有特征:
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 ) }
}
然后我用这个特征创建scala类:
import _root_.com.thoughtworks.selenium._
class WebAppField(sel:Selenium, nam: String) extends SeleniumField {
def selenium = sel
override val name = nam
}
然后当我尝试在代码中使用它时:
val rodzaj = new WebAppField(selenium, "RODZAJ")
rodzaj text should equal "K"
我得到:
error: not found: value should
[INFO] rodzaj text should equal "K"
我做错了什么?
Scala 版本 2.8
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当您在 Scala 中的方法调用中省略点和括号时,它们总是以相同的方式进行解析,假设中缀表示法和单个参数。
与
尝试将其重写为:
或者您可以完全标点为:
When you omit dots and parentheses from method calls in Scala, they are always parsed the same way, assuming infix notation and single arguments.
is the same as
Try rewriting it as:
or you could fully punctuate as:
字符串通常没有
should
方法。 ScalaTest 通过隐式转换使其可用。在您编写测试的地方,您需要此导入:
将其隐含到范围内。导入出现在正在测试的代码中是不够的。
实际上,在正在测试的代码中引用 ScalaTest 有点奇怪。通常对测试框架的引用应该只出现在您的测试中。
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:
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.