2012-02-01 9 views
5

私は簡単なスニペットを作っていますが、リクエストuser-agentを持つBox[String]をhtml要素に追加する必要があるcssクラスを返すヘルパークラスに渡す必要があります。私はリフトにhtml5boilerplateのような条件付きコメントを提供することは難しいようですが、これをやっています。これは私が今持っている、それがどのような作品です:リフトでスニペットをテストする

class LiftBoilerplate { 

    def render = "html [class+]" #> getClassForUserAgent(S.request) 

    private def getClassForUserAgent(request:Box[Req]) = request match { 
     case Full(r) => LiftBoilerplateHelper.getHtmlClass(r.userAgent) 
     case _ => "" 
    } 
} 

私の問題は、私はこれ等のユニットテストを書きたいのですがということです:

object LiftBoilerplateSpecs extends Specification { 

    val session = new LiftSession("", randomString(20), Empty) 

    "LiftBoilerplate" should { 
    "add 'no-js' to the class of an html tag element" in { 

     val snippet = new LiftBoilerplate 
     val result = snippet.render(<html><head></head><body>test</body></html>) 

     result must ==/(<html class="no-js"><head></head><body>test</body></html>) 
    } 
    } 
} 

S.requestEmptyであるため、このテストが失敗しました。スニペットにuserAgentを含む疑惑のあるリクエストを与えるにはどうしたらよいですか?

これまでのところ私はhttp://www.assembla.com/spaces/liftweb/wiki/Unit_Testing_Snippets_With_A_Logged_In_User

http://www.assembla.com/spaces/liftweb/wiki/Mocking_HTTP_Requests
を見てきましたが、私は私の目標をachiveする方法を理解していません。リクエストを作成し、あなたがS.init呼び出しで各テストをラップする形質AroundExampleを使用する必要があります各試験例では、それを自動的に適用するには

答えて

3

object LiftBoilerplateSpecs extends Specification with AroundExample { 

    val session = new LiftSession("", randomString(20), Empty) 

    def makeReq = { 
    val mockRequest = new MockHttpServletRequest("http://localhost") 
    mockRequest.headers = Map("User-Agent" -> List("Safari")) 

    new Req(Req.NilPath, "", GetRequest, Empty, new HTTPRequestServlet(mockRequest, null), 
     System.nanoTime, System.nanoTime, false, 
    () => ParamCalcInfo(Nil, Map(), Nil, Empty), Map()) 
    } 

    def around[T <% Result](t: => T) = S.init(makeReq, session)(t) 

    "LiftBoilerplate" should { 
    "add 'no-js' to the class of an html tag element" in { 

     val snippet = new LiftBoilerplate 
     val result = snippet.render(<html><head></head><body>test</body></html>) 

     result must ==/(<html class="no-js"><head></head><body>test</body></html>) 
    } 
    } 
} 
+0

優れたが、ユーザーエージェントのセットです? 'ParamCalcInfo'に与えられた' Map'で追加するべきでしょうか? –

+0

いいえ、Req()の5番目のパラメータはHttpRequestです。そのインスタンスに目的のヘッダー(例:User-Agent)を渡す必要があります –

+0

虚偽のリクエストを表示するための回答が更新されました。 –

関連する問題