【问题标题】:How to test snippet code in liftweb using specs2 framework如何使用 specs2 框架在 liftweb 中测试代码片段
【发布时间】:2013-10-01 02:38:11
【问题描述】:

我正在尝试编写一个 Specs2 测试用例来测试 sn-ps。 我的 sn-p 看起来像这样:

class RegisterTest extends Specification {
  val testurl = "http:/html/register?username=liftvalues"
  val testSession = MockWeb.testS(testurl) { S.session }
  def inSession[T](a: => T): T = S.initIfUninitted(testSession) { a }
  def is = s2"""   example1 $e1   """
  val html = <form><input name="username" value="liftvalues"></input></form>
  def e1 = {
    inSession{
      register(html)
    }
  }  
  def register(in:NodeSeq):Result = {
      val username = S.param("username") //Here we are getting "Empty Value" for the S object. 
      username === "liftvalues"  and  UserSchemaTest.registerData("data")
  }
}

此测试失败,因为 S.paramEmpty。我应该怎么做才能向 sn-p 提供模拟请求?

到目前为止,我已经看过Unit Testing Snippets With A Logged In UserMocking HTTP Requests,但我不明白如何实现我的目标。

【问题讨论】:

    标签: scala lift specs2


    【解决方案1】:

    您的代码甚至不应该编译,因为除其他外,testSession 会返回一个Box[LiftSession],而S.initIfUninitted 需要一个未装箱的LiftSession。此外,这甚至不需要,因为MockWeb.testS 将为您初始化会话,see here

    我对 Specs2 不是很熟悉,但我相信这样的事情应该可以满足您的需求,或者至少可以让您接近:

    class RegisterTest extends Specification {
    
      val testurl = "http://html/register?username=liftvalues"
    
      val html = <form><input name="username" value="liftvalues"></input></form>
      def e1 = register(html)  
    
      def register(in:NodeSeq):Boolean = {
          val username = S.param("username") //Here we are getting "Empty Value" for the S object. 
          username === "liftvalues" and  UserSchemaTest.registerData("data")
      }
    
      MockWeb.testS(testurl) {
        s2"""   example1 $e1   """
      }
    
    }
    

    MockWeb.testS 块中调用的所有内容都应该可以访问您的会话和请求 - 这样您就可以正常调用您的方法。

    另外,您的测试看起来也有问题,s2""" 可能会抛出错误。但是,我不完全确定你想要它做什么,所以我无法提出替代方案。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-09-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-19
      • 1970-01-01
      相关资源
      最近更新 更多