在光滑标量中测试DAL时出错



我对scala和play很陌生,我被分配了一项任务来测试其他人的应用程序,该应用程序运行良好。请检查我的测试是否正确,错误是什么。

这是型号中的employeeEntry.scala文件

 package models
import models.database.Employee
import play.api.db.slick.Config.driver.simple._
import play.api.db.slick._
import play.api.Play.current

case class EmployeeEntry(eid :Int, ename: String, eadd: String, emob: String)
object Employee {
val DBemp = TableQuery[Employee]

def savedat(value: EmployeeEntry):Long = {
    DB.withSession { implicit session =>
       DBemp+=EmployeeEntry(eid=value.eid,ename=value.ename,eadd=value.eadd,emob=value.emob)
    }}
/*val query = for (c <- Employee) yield c.ename 
    val result = DB.withSession {
                                 implicit session =>
                                 query.list // <- takes session implicitly
                                }*/
        //val query = for (c <- Employee) yield c.ename
   def getPersonList: List[EmployeeEntry] = DB.withSession { implicit session =>      DBemp.list }
  def Update: Int = DB.withSession { implicit session =>
 (DBemp filter (_.eid === 1) map (s => (s.ename,s.eadd))) update ("test","khair")}
  def delet :Int =DB.withSession {
  implicit session => (DBemp filter (_.eid === 1)).delete
}



 }

这是模型/数据库中的Employee.scala文件

 package models.database
 import models._
 import models.EmployeeEntry
 import play.api.db.slick.Config.driver.simple._
 import scala.slick.lifted._
 class Employee(tag:Tag) extends Table[EmployeeEntry](tag,"employee") 
 {

   //val a = "hello"
  def eid = column[Int]("eid", O.PrimaryKey)
  def ename = column[String]("name", O.DBType("VARCHAR(50)"))
  def emob = column[String]("emob",O.DBType("VARCHAR(10)"))
  def eadd =column[String]("eadd",O.DBType("VARCHAR(100)"))
  def * = (eid, ename, emob, eadd) <> (EmployeeEntry.tupled, EmployeeEntry.unapply)

 }

最后,这是我正在运行的测试,它失败了:

 import play.api.db.slick.Config.driver.simple._
 import play.api.db.slick._
 import play.api.Play.current

 import org.scalatest.FunSpec
 import org.scalatest.matchers.ShouldMatchers
 import models.database.Employee
 import scala.slick.lifted._ 
 import models._
 import models.EmployeeEntry
 //import scala.slick.driver.H2Driver.simple._

 class databasetest extends FunSpec with ShouldMatchers{

   describe("this is to check database layer"){ 
       it("can save a row"){
            val a = EmployeeEntry(1006,"udit","schd","90909090")
            Employee.savedat(a) should be (1)

      }     

     it("getpersonlist"){

            Employee.getPersonList.size should be (1)

    }

     }
  }

测试失败,错误为

  java.lang.RuntimeException: There is no started application
  at scala.sys.package$.error(package.scala:27)
  at play.api.Play$$anonfun$current$1.apply(Play.scala:71)
  at play.api.Play$$anonfun$current$1.apply(Play.scala:71)
  at scala.Option.getOrElse(Option.scala:120)
  at play.api.Play$.current(Play.scala:71)
  at models.Employee$.getPersonList(EmployeeEntry.scala:27)
  at databasetest$$anonfun$1$$anonfun$apply$mcV$sp$2.apply$mcV$sp(databasetest.scala:39)
  at databasetest$$anonfun$1$$anonfun$apply$mcV$sp$2.apply(databasetest.scala:39)
  at databasetest$$anonfun$1$$anonfun$apply$mcV$sp$2.apply(databasetest.scala:39)
  at org.scalatest.Transformer$$anonfun$apply$1.apply$mcV$sp(Transformer.scala:22)

默认情况下,play提供spec2测试框架。因此,无需添加scalatest框架进行单元测试。对于数据库访问层测试,需要与数据库连接,所以启动一个假的应用程序。(这不是运行代码,只是编写单元测试的想法)有关更多详细信息,请参阅look play文档:https://www.playframework.com/documentation/2.3.x/ScalaFunctionalTestingWithSpecs2

import org.specs2.mutable.Specification
import models.database.Employee
import models._
import models.EmployeeEntry
import play.api.test.FakeApplication
import play.api.test.Helpers.running
import play.api.Play.current

class databasetest extends Specification {
 "database layer" should {
     "save a row" in {
        running(FakeApplication()) {
           val a = EmployeeEntry(1006,"udit","schd","90909090")
           Employee.savedat(a) must be equalTo (1)
         }
       }
    "get list" in {
         running(FakeApplication()) {
           Employee.getPersonList.size must be equalTo (1)
         }
     }
}

最新更新