i am following a tutorial to create a Producer Consumer with actors
i made three files i.e Master.scala, Counter.scala and Parser.scala
i am getting not found value in two of my files(Counter.scala and Parser.scala)
Parser.scala
import akka.actor.Actor
import akka.actor.ActorSystem
import akka.actor.Props
import akka.actor._
case object Processed
class Parser(counter: ActorRef) extends Actor {
val pages=Pages(100000, "enwiki.xml")
override def preStart{
for(page<-pages.take(10))
counter ! page
}
def receive={
case Processed if pages.hasNext => counter ! pages.next
case _ => context.stop(self)
}
}
Counter.scala
import akka.actor.Actor
import akka.actor.ActorSystem
import akka.actor.Props
import akka.actor._
class Counter extends Actor {
val counts = HashMap[String,Int]().withDefaultValue(0)
def receive={
case Page(title, text)=>
for (word<-Words(text))
counts(word) += 1
sender ! Processed
}
}
Errors
[error] /home/ahsen/SbtPrctc/ProducerConsumer/src/main/scala/Counter.scala:6: not found: value HashMap
[error] val counts = HashMapString,Int.withDefaultValue(0)
[error] ^
[error] /home/ahsen/SbtPrctc/ProducerConsumer/src/main/scala/Counter.scala:8: not found: value Page
[error] case Page(title, text)=>
[error] ^
[error] /home/ahsen/SbtPrctc/ProducerConsumer/src/main/scala/Counter.scala:9: not found: value Words
[error] for (word<-Words(text))
[error] ^
[error] /home/ahsen/SbtPrctc/ProducerConsumer/src/main/scala/Parser.scala:7: not found: value Pages
[error] val pages=Pages(100000, "enwiki.xml")
[error] ^
i am sure i am doing some stupid mistake bt please help me out
If you want to use the Java HashMap you need to import java.util.HashMap. However, I believe in your case you want to use the Scala scala.collections.mutable.Map (which you also need to import, and change HashMap to Map).
Also, please properly indent your code in your questions, and also mark the stack traces as code. It makes it much easier for respondents to read your questions.
Related
I want to verify the order of sequence calls, but it didn't work as I expected.
import akka.actor.ActorSystem
import akka.testkit.TestKit
import org.scalatest._
import org.specs2.mock.Mockito
class Test extends TestKit(ActorSystem("testSystem"))
with WordSpecLike
with BeforeAndAfterAll
with PrivateMethodTester
with `enter code here`Mockito
{
val m = mock[java.util.List[String]]
m.get(0) returns "one"
there was two(m).get(2) //do not throw any error, why???
}
I'm using
scala 2.11.7,
specs2-core 3.6.6,
specs2-mock 3.6.6,
scalatest 2.2.4
thx
I don't think you can mix Specs2 and ScalaTest.
You shuld remove import org.scalatest._ and use import org.specs2.mutable.SpecificationLike instead.
import akka.testkit.TestKit
import akka.actor.ActorSystem
import org.specs2.mock.Mockito
import org.specs2.mutable.SpecificationLike
class Test extends TestKit(ActorSystem("testSystem"))
with Mockito
with SpecificationLike
{
"it" should{
"raise error" in {
val m = mock[java.util.List[String]]
m.get(0) returns "one"
there was two(m).get(2)
}
}
}
Now you can see that sbt test returns something like.
[error] The mock was not called as expected:
[error] Wanted but not invoked:
[error] list.get(2);
[error] -> at Test$$anonfun$1$$anonfun$apply$1$$anonfun$apply$3.apply(Test.scala:14)
[error] Actually, there were zero interactions with this mock. (Test.scala:14)
Here is an example from the scala.rx doc:
package tutorial.webapp
import rx.core.{Rx, Var}
import rx._
import rx.ops._
import scala.concurrent.Promise
import scala.concurrent.duration._
import scala.scalajs.js.JSApp
import scala.scalajs.js.annotation.JSExport
import scala.concurrent.ExecutionContext.Implicits.global
/**
* Created by IDEA on 29/10/15.
*/
object RxAddtionalOps extends JSApp {
#JSExport
override def main(): Unit = {
mapDemo
filterDemo
asyncDemo
async2
timer1
}
def delay1: Unit = {
val a = Var(10)
val b = a.delay(250 millis)
a() = 5
println(b())
eventually{
println(b)
}
}
}
I got this error on sbt when compiling:
[error] /Users/kaiyin/personal_config_bin_files/workspace/scalajsHandson/src/main/scala/tutorial/webapp/RxAddtionalOps.scala:43: not found: value eventually
[error] eventually{
[error] ^
[error] one error found
[error] (compile:compileIncremental) Compilation failed
[error] Total time: 1 s, completed 30 oct. 2015 11:15:07
Where does the eventually function come from? Am I missing any imports?
It's defined in utest, which is a author's framework for tests. Since it's a test dependency it doesn't come bundled with scalarx. BTW, the very same functionality presented in scalatest.
I'm new to scala but I'm trying to add some tests to a simple file. The problem is that when I try to import the object into the test I get "error: not found: value"
PerfectNumberImperative.scala
package perfectImperative
package object perfectImperative{
def main(args: Array[String]){
perfectImperative(args(0).toInt)
}
def perfectImperative(input: Int): Boolean = {
var evaluation = (for (possibleFactor <-1 to input if (input % possibleFactor == 0)) yield possibleFactor).sum == input*2
return evaluation
}
}
PerfectNumberSpec.scala
import org.scalatest.FlatSpec
import perfectImperative._
class PerfectNumberSpec extends FlatSpec {
it should "return true for 6" in {
assert(perfectImperative.perfectImperative(6))
}
}
[error] C:\Users\Daniel\PerfectNumbersScala\src\test\scala\PerfectNumberSpec.scala:2: not found: object perfectImperative
[error] import perfectImperative._
[error] ^
[error] C:\Users\Daniel\PerfectNumbersScala\src\test\scala\PerfectNumberSpec.scala:6: not found: value perfectImperative
[error] assert(perfectImperative.perfectImperative(6))
[error] ^
[error] two errors found
[error] (test:compileIncremental) Compilation failed
[error] Total time: 2 s, completed Sep 16, 2015 2:30:38 AM
I would appreciate if someone could point me in the right direction.
Test
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())
}
}
Code
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
}
Outcome
value not a member of object
not enough arguments for method apply: (index: Int)Char in class StringOps
test
[info] Compiling 1 Scala source to C:\path\to\developme
nt\scalaNumberSequences\target\scala-2.10\test-classes...
[error] C:\path\to\development\scalaNumberSequences\src
\test\scala\com\utrecht\numbersequences\NumberSequencesTest.scala:32: value city
_ is not a member of object com.utrecht.numbersequences.NumberSequences
[error] NumberSequences.city_("utrecht")
[error] ^
[error] C:\path\to\development\scalaNumberSequences\src
\test\scala\com\utrecht\numbersequences\NumberSequencesTest.scala:33: not enough
arguments for method apply: (index: Int)Char in class StringOps.
[error] Unspecified value parameter index.
[error] assert("utrecht" === NumberSequences.city())
[error] ^
[error] two errors found
[error] (test:compile) Compilation failed
[error] Total time: 1 s, completed Aug 10, 2014 5:52:16 PM
NumberSequences.city_=("utrecht")
//OR
NumberSequences.city = "utrecht"
but not:
NumberSequences.city_("utrecht") // city_ is not a method existing in the object
Could someone please help. I have Play2 project in which I need to test some DAO code.
I used documentaion from http://www.playframework.org/documentation/2.0.2/ScalaTest.
The test is very simple:
import models.Calendar
import org.specs2.mutable._
import play.api.test._
import play.api.test.Helpers._
class CalendarSpec extends Specification {
"Calendar model" should {
"be retrieved by id" in {
val fakeApp = FakeApplication()
running(fakeApp) {
lazy val calendarId= Calendar.addCalendar(
Calendar(subject="test",
upAccount = "mytest",
masterId = 1,
calendarType = 1,
isAllDayEvent = false,
hasAttachment = false,
category = "test",
instanceType = 1,
upName = "test" ))
lazy val Some(calendar) = Calendar.getCalendar(calendarId.get)
calendar.upAccount must equalTo("mytest")
}
}
}
}
And when I run 'sbt test' I get strange error:
[info] Calendar model should
[error] ! Fragment evaluation error
[error] ThrowableException: play.api.test.Helpers$.play$api$http$HeaderNames$_setter_$ACCESS_CONTROL_ALLOW_ORIGIN_$eq(Ljava/lang/String;)V (TraversableLike.scala:194)
[error] play.api.http.HeaderNames$class.$init$(StandardValues.scala:195)
[error] play.api.test.Helpers$.<init>(Helpers.scala:16)
[error] play.api.test.Helpers$.<clinit>(Helpers.scala:111)
[error] CalendarSpec$$anonfun$1$$anonfun$apply$1.apply(CalendarSpec.scala:13)
[error] CalendarSpec$$anonfun$1$$anonfun$apply$1.apply(CalendarSpec.scala:10)
[error] play.api.test.Helpers$.play$api$http$HeaderNames$_setter_$ACCESS_CONTROL_ALLOW_ORIGIN_$eq(Ljava/lang/String;)V
[error] play.api.http.HeaderNames$class.$init$(StandardValues.scala:195)
[error] play.api.test.Helpers$.<init>(Helpers.scala:16)
[error] play.api.test.Helpers$.<clinit>(Helpers.scala:111)
[error] CalendarSpec$$anonfun$1$$anonfun$apply$1.apply(CalendarSpec.scala:13)
[error] CalendarSpec$$anonfun$1$$anonfun$apply$1.apply(CalendarSpec.scala:10)
StackOverflow/Google knows nothing about this exception. Thanks in advance.
The stacktrace makes me think that a library is incorrect or missing in your classpath. This is why you are seeing "Helpers$." traces where the class constructor seems to be failing.
You can validate this by writing a small app in your test directory, without specs2 but using Play2's helper classes and see what happens.
I found solution - https://groups.google.com/forum/#!msg/play-framework/NSN9xfktUks/EwiG1Cc0C9oJ:
new play.core.StaticApplication(new java.io.File(".")) should be added to actualy start Play app so DAO calls can work in test.