Cannot get the result with Spec2 (Play Framework)

25 views
Skip to first unread message

Thai Tran

unread,
Apr 17, 2014, 2:26:28 AM4/17/14
to reacti...@googlegroups.com
I am implementing the unit tests for my model

// DailyQuotesCategor.scala
object DailyQuotesCategory extends DBModel {  
  val collection
= connect("dqCategory")
 
implicit object DQCategoryBSONReader extends BSONDocumentReader[DQCategory] {
   
....
 
}

 
implicit object DQSentenceBSONWriter extends BSONDocumentWriter[DQCategory] {
   
....
 
}

 
def findByTitleWithoutSentences (title: String): Future[List[DQCategory]] = {
    val query
= BSONDocument(
     
"title" -> title
   
)

    val filter
= BSONDocument (
     
"sentences" -> 0
   
)

    collection
.find(query, filter).cursor[DQCategory].collect[List]()
 
}


With my Controller

// DQCategoryCtrl.scala
object DQCategoryCtrl  extends Controller with MongoController {

 
def list = Action.async { implicit request =>
    val result
= DQCategory.findByTitleWithoutSentences("test")

    result
.map { categories =>
     
for(category <- categories) {
        val title
= category.title
        println(title)
     
}
   
}

    result
.map { categories =>
     
Ok(views.html.dq.category.list(categories))
   
}.recover {
     
case e =>
       
Logger.error(e.getMessage)
       
BadRequest(e.getMessage)
   
}
 
}

console will print out the value of the result (1 value). However, when running in the testing environment:

// DQCategorySpec .scala
@RunWith(classOf[JUnitRunner])
class DQCategorySpec extends Specification {

 
"Listing 1 category " should {
   
"return 1 value" in new WithApplication {

      val result
= DQCategory.findByTitleWithoutSentences("test")

      result
.map {
        categories
=>
       
for(category <- categories) {
          val title
= category.title
          println(title)
       
}
     
}
   
}
 
}
}

console does not print out anything. Could you give some suggestion about this ?


Reply all
Reply to author
Forward
0 new messages