Dustin,
I've updated the Basic archetype to include a couple of things:
- An example of Lift's new Factory support which blends the best of
dependency injection and and highly configurable, type-safe factories
- An example of how to set up a session to test a snippet
- An example of injecting a test value into the snippet test via the
factory
The test that you asked about looks like:
object HelloWorldTestSpecs extends Specification {
val session = new LiftSession("", randomString(20), Empty)
"HelloWorld Snippet" should {
"Put the time in the node" in {
S.initIfUninitted(session) {
val stableTime = now
DependencyFactory.time.doWith(stableTime) {
val hello = new HelloWorld
Thread.sleep(1000) // make sure the time changes
val str = hello.howdy(<span>Hello at <b:time/></span>).toString
str.indexOf(stableTime.toString) must be >= 0
str.indexOf("Hello at") must be >= 0
}
}
}
}
}
This demonstrates creating a mock LiftSession and setting up S with
"initIfUnitted".
The DepencyFactory.time.doWith sets up a new value to be generated from the
DepencyFactory.time object (rather than using the global default function,
the stableTime value is used.)
In the HelloWord snippet, we inject the value of the time object:
class HelloWorld {
import DependencyFactory._ // Import the Dependency Injector
val date = inject[Date] // inject the date
// lazy val date = DependencyFactory.time.make // create the date via
factory
def howdy(in: NodeSeq): NodeSeq =
Helpers.bind("b", in, "time" -> date.map(d => Text(d.toString)))
}
Note there are two ways of getting the time. The inject method will locate
a Maker for a java.util.Date. This is a whole lot less (in my opinion)
optimal than the second option, which explicitly calls the time factory. In
either case, the current time Maker (global, session-specific,
request-specific, and stack-specific) is consulted.
To build the Factory itself:
object DependencyFactory extends Factory {
implicit object time extends FactoryMaker(Helpers.now _)
}
I hope this answers you question about testing snippets and more broadly
addresses some of the recent discussions on the list about Dependency
Injection, Factories, mocks, and testing.
Thanks,
David
On Wed, Sep 9, 2009 at 1:05 PM, Dustin Whitney <[email protected]>wrote:
> All,
>
> I am having trouble finding any literature on how to test a Lift app.
> I've got a snippet. I'd like to mock a request and test the NodeSeq that
> comes back from the render method in my snippet. Can anyone point me to
> some code that's doing that?
>
> Thanks,
> Dustin
>
> >
>
--
Lift, the simply functional web framework http://liftweb.net
Beginning Scala http://www.apress.com/book/view/1430219890
Follow me: http://twitter.com/dpp
Git some: http://github.com/dpp
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Lift" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/liftweb?hl=en
-~----------~----~----~----~------~----~------~--~---