[英]How to test snippet code in liftweb using specs2 framework
I'm trying to write a Specs2 test case that will test the snippets. 我正在尝试编写一个Specs2测试用例,以测试代码片段。 My snippet would look something like this:
我的代码段看起来像这样:
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")
}
}
This test fails since S.param
is Empty
. 由于
S.param
为Empty
所以该测试失败。 What should I do to supply the snippet with a mocked Request? 如何为摘要提供模拟请求?
So far I have looked at Unit Testing Snippets With A Logged In User and Mocking HTTP Requests , but I do not understand how to achive my goal. 到目前为止,我已经研究了具有登录用户和模拟HTTP请求的 单元测试摘要 ,但是我不知道如何实现我的目标。
Your code as-is shouldn't even compile, since among other things testSession
would return a Box[LiftSession]
and S.initIfUninitted
requires an unboxed LiftSession
. 您的代码按原样甚至不应该编译,因为
testSession
除其他外将返回Box[LiftSession]
而S.initIfUninitted
需要一个未装箱的LiftSession
。 Also, that shouldn't even be needed since MockWeb.testS
will initialize the session for you, see here . 另外,甚至不需要,因为
MockWeb.testS
将为您初始化会话, 请参见此处 。
I'm not super familiar with Specs2, but I believe something like this should do what you want or at least get you close: 我对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 """
}
}
Everything that gets called from within the MockWeb.testS
block should have access to your session and request - so you'd be able to make your method calls normally. 从
MockWeb.testS
块中调用的MockWeb.testS
都应有权访问您的会话和请求-这样您就可以正常进行方法调用。
Also, your test also looks wrong, a s2"""
will probably throw an error. But, I'm not entirely sure what you are wanting it to do so I couldn't suggest an alternative. 另外,您的测试也看起来错误,
s2"""
可能会引发错误。但是,我不确定您要执行的操作,因此我不建议您使用其他方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.