[Lift] Re: Testing snippets that depend on a user logged in
Hi all, I added a specs version as well. Eric. On Oct 2, 2:37 pm, Bill Venners b...@artima.com wrote: Hi Ryan, David, Eric, I added a ScalaTest version to your wiki page: http://wiki.github.com/dpp/liftweb/how-to-unit-test-lift-snippets-wit... Eric you may want to add a specs version. Bill On Thu, Oct 1, 2009 at 3:03 PM, rstradling ryanstradl...@gmail.com wrote: Awesome!!! Thanks guys for the help. It now works. I put a how-to wiki document up on github. For me this was one of those times where my google searches did not seem to turn up anything fruitful, so I thought this how-to would be helpful. If it is not helpful, then no hard feelings if the page is deleted. I just wanted to give back. Wiki page http://wiki.github.com/dpp/liftweb/how-to-unit-test-lift-snippets-wit... On Oct 1, 4:53 pm, David Pollak feeder.of.the.be...@gmail.com wrote: Bill, Thanks for posting this. I am, by experience (I started using it, I can use it enough to write basic tests, I know no more) using Specs. I would welcome and encourage some sample tests in Lift archetypes that use ScalaTest. I want to make sure that folks who pick up Lift get to experience ScalaTest as well as Specs... that way, folks who have a better understanding of tests can make better choices. Thanks, David On Thu, Oct 1, 2009 at 1:27 PM, Bill Venners b...@artima.com wrote: Hi Ryan, It looks like you're currently using a JUnit TestCase. If you want an easier port to something that would work you could use a ScalaTest Suite like this: import org.scalatest.Suite class YourSuite extends Suite { val session = new LiftSession(, randomString(20), Empty) val stableTime = now override def withFixture(test: NoArgTest) { S.initIfUninitted(session) { val user = User.create user.firstName(XXX) user.lastName(YYY) user.save User.logUserIn(user) test() } } def testValue() { val xml = xml:group tr td p:fullNameMy Name/p:fullName /td td p:styleFighter Style/p:style /td td p:weightWeight/p:weight /td /tr /xml:group val trainer = new Trainer() val output = trainer.showPeople(xml) // seems like you need an assertion here... } } A Suite considers methods that start with test as tests, like JUnit 3, except they don't need to result in Unit so you don't need an extra () at the end. The withFixture method is an alternative to beforeEach/afterEach, which are like JUnit 3's setUp/tearDown methods. Each test gets passed as a function to withFixture, which is responsible for executing the test by invoking the function. In this case, it is executed in the context created by S. initIfUninitted. This is ScalaTest 1.0, which is available as a SNAPSHOT right now but should be released proper a week from Monday. http://www.artima.com/scalatest Bill On Thu, Oct 1, 2009 at 9:50 AM, David Pollak feeder.of.the.be...@gmail.com wrote: Using Specs 1.6: object HelloWorldTestSpecs extends Specification { val session = new LiftSession(, randomString(20), Empty) val stableTime = now override def executeExpectations(ex: Examples, t: =Any): Any = { S.initIfUninitted(session) { ... put your User init here. The User.logUserIn will be within the context of a session and thus session (and request) vars will be valid } } HelloWorld Snippet should { Put the time in the node in { ... do testing here } } } Hope this helps. On Thu, Oct 1, 2009 at 8:55 AM, rstradling ryanstradl...@gmail.com wrote: I have a class called class Trainer { def showPeople(xhtml : Group) : NodeSeq = { val user : User = User.currentUser.open_! ... } } I then want to write a unit test to test that returns proper xml. The test is written as so def testValue() = { val xml = xml:group tr td p:fullNameMy Name/p:fullName /td td p:styleFighter Style/p:style /td td p:weightWeight/p:weight /td /tr /xml:group val trainer = new Trainer() val output = trainer.showPeople(xml) () } The User object inherits from MegaProtoUser. The problem is I am not sure how to create a mock user and sign them in. I have tried in my unit test override def setUp : Unit = { val user = User.create user.firstName(XXX) user.lastName(YYY) user.save User.logUserIn(user)
[Lift] Re: Testing snippets that depend on a user logged in
Sweet!!! Thanks Bill for adding a ScalaTest version. I wanted to but just did not find the time yesterday. I also don't know ScalaTest very well but will be working with it shortly. On Oct 2, 12:37 am, Bill Venners b...@artima.com wrote: Hi Ryan, David, Eric, I added a ScalaTest version to your wiki page: http://wiki.github.com/dpp/liftweb/how-to-unit-test-lift-snippets-wit... Eric you may want to add a specs version. Bill On Thu, Oct 1, 2009 at 3:03 PM, rstradling ryanstradl...@gmail.com wrote: Awesome!!! Thanks guys for the help. It now works. I put a how-to wiki document up on github. For me this was one of those times where my google searches did not seem to turn up anything fruitful, so I thought this how-to would be helpful. If it is not helpful, then no hard feelings if the page is deleted. I just wanted to give back. Wiki page http://wiki.github.com/dpp/liftweb/how-to-unit-test-lift-snippets-wit... On Oct 1, 4:53 pm, David Pollak feeder.of.the.be...@gmail.com wrote: Bill, Thanks for posting this. I am, by experience (I started using it, I can use it enough to write basic tests, I know no more) using Specs. I would welcome and encourage some sample tests in Lift archetypes that use ScalaTest. I want to make sure that folks who pick up Lift get to experience ScalaTest as well as Specs... that way, folks who have a better understanding of tests can make better choices. Thanks, David On Thu, Oct 1, 2009 at 1:27 PM, Bill Venners b...@artima.com wrote: Hi Ryan, It looks like you're currently using a JUnit TestCase. If you want an easier port to something that would work you could use a ScalaTest Suite like this: import org.scalatest.Suite class YourSuite extends Suite { val session = new LiftSession(, randomString(20), Empty) val stableTime = now override def withFixture(test: NoArgTest) { S.initIfUninitted(session) { val user = User.create user.firstName(XXX) user.lastName(YYY) user.save User.logUserIn(user) test() } } def testValue() { val xml = xml:group tr td p:fullNameMy Name/p:fullName /td td p:styleFighter Style/p:style /td td p:weightWeight/p:weight /td /tr /xml:group val trainer = new Trainer() val output = trainer.showPeople(xml) // seems like you need an assertion here... } } A Suite considers methods that start with test as tests, like JUnit 3, except they don't need to result in Unit so you don't need an extra () at the end. The withFixture method is an alternative to beforeEach/afterEach, which are like JUnit 3's setUp/tearDown methods. Each test gets passed as a function to withFixture, which is responsible for executing the test by invoking the function. In this case, it is executed in the context created by S. initIfUninitted. This is ScalaTest 1.0, which is available as a SNAPSHOT right now but should be released proper a week from Monday. http://www.artima.com/scalatest Bill On Thu, Oct 1, 2009 at 9:50 AM, David Pollak feeder.of.the.be...@gmail.com wrote: Using Specs 1.6: object HelloWorldTestSpecs extends Specification { val session = new LiftSession(, randomString(20), Empty) val stableTime = now override def executeExpectations(ex: Examples, t: =Any): Any = { S.initIfUninitted(session) { ... put your User init here. The User.logUserIn will be within the context of a session and thus session (and request) vars will be valid } } HelloWorld Snippet should { Put the time in the node in { ... do testing here } } } Hope this helps. On Thu, Oct 1, 2009 at 8:55 AM, rstradling ryanstradl...@gmail.com wrote: I have a class called class Trainer { def showPeople(xhtml : Group) : NodeSeq = { val user : User = User.currentUser.open_! ... } } I then want to write a unit test to test that returns proper xml. The test is written as so def testValue() = { val xml = xml:group tr td p:fullNameMy Name/p:fullName /td td p:styleFighter Style/p:style /td td p:weightWeight/p:weight /td /tr /xml:group val trainer = new Trainer() val output = trainer.showPeople(xml) () } The User object inherits from MegaProtoUser. The problem is I am not sure how to create a mock user and sign them in. I have tried in my unit test override def setUp :
[Lift] Re: Testing snippets that depend on a user logged in
Using Specs 1.6: object HelloWorldTestSpecs extends Specification { val session = new LiftSession(, randomString(20), Empty) val stableTime = now override def executeExpectations(ex: Examples, t: =Any): Any = { S.initIfUninitted(session) { ... put your User init here. The User.logUserIn will be within the context of a session and thus session (and request) vars will be valid } } HelloWorld Snippet should { Put the time in the node in { ... do testing here } } } Hope this helps. On Thu, Oct 1, 2009 at 8:55 AM, rstradling ryanstradl...@gmail.com wrote: I have a class called class Trainer { def showPeople(xhtml : Group) : NodeSeq = { val user : User = User.currentUser.open_! ... } } I then want to write a unit test to test that returns proper xml. The test is written as so def testValue() = { val xml = xml:group tr td p:fullNameMy Name/p:fullName /td td p:styleFighter Style/p:style /td td p:weightWeight/p:weight /td /tr /xml:group val trainer = new Trainer() val output = trainer.showPeople(xml) () } The User object inherits from MegaProtoUser. The problem is I am not sure how to create a mock user and sign them in. I have tried in my unit test override def setUp : Unit = { val user = User.create user.firstName(XXX) user.lastName(YYY) user.save User.logUserIn(user) } The mock user log-in *seems* to work fine but when User.currentUser.open_! is called it throws an exception on trying to open an empty box. So either how do I do this or how do others do this type of testing. I am sure I am missing something simple. Thanks, ryan -- Lift, the simply functional web framework http://liftweb.net Beginning Scala http://www.apress.com/book/view/1430219890 Follow me: http://twitter.com/dpp Surf the harmonics --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Lift group. To post to this group, send email to liftweb@googlegroups.com To unsubscribe from this group, send email to liftweb+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/liftweb?hl=en -~--~~~~--~~--~--~---
[Lift] Re: Testing snippets that depend on a user logged in
Hi Ryan, It looks like you're currently using a JUnit TestCase. If you want an easier port to something that would work you could use a ScalaTest Suite like this: import org.scalatest.Suite class YourSuite extends Suite { val session = new LiftSession(, randomString(20), Empty) val stableTime = now override def withFixture(test: NoArgTest) { S.initIfUninitted(session) { val user = User.create user.firstName(XXX) user.lastName(YYY) user.save User.logUserIn(user) test() } } def testValue() { val xml = xml:group tr td p:fullNameMy Name/p:fullName /td td p:styleFighter Style/p:style /td td p:weightWeight/p:weight /td /tr /xml:group val trainer = new Trainer() val output = trainer.showPeople(xml) // seems like you need an assertion here... } } A Suite considers methods that start with test as tests, like JUnit 3, except they don't need to result in Unit so you don't need an extra () at the end. The withFixture method is an alternative to beforeEach/afterEach, which are like JUnit 3's setUp/tearDown methods. Each test gets passed as a function to withFixture, which is responsible for executing the test by invoking the function. In this case, it is executed in the context created by S. initIfUninitted. This is ScalaTest 1.0, which is available as a SNAPSHOT right now but should be released proper a week from Monday. http://www.artima.com/scalatest Bill On Thu, Oct 1, 2009 at 9:50 AM, David Pollak feeder.of.the.be...@gmail.com wrote: Using Specs 1.6: object HelloWorldTestSpecs extends Specification { val session = new LiftSession(, randomString(20), Empty) val stableTime = now override def executeExpectations(ex: Examples, t: =Any): Any = { S.initIfUninitted(session) { ... put your User init here. The User.logUserIn will be within the context of a session and thus session (and request) vars will be valid } } HelloWorld Snippet should { Put the time in the node in { ... do testing here } } } Hope this helps. On Thu, Oct 1, 2009 at 8:55 AM, rstradling ryanstradl...@gmail.com wrote: I have a class called class Trainer { def showPeople(xhtml : Group) : NodeSeq = { val user : User = User.currentUser.open_! ... } } I then want to write a unit test to test that returns proper xml. The test is written as so def testValue() = { val xml = xml:group tr td p:fullNameMy Name/p:fullName /td td p:styleFighter Style/p:style /td td p:weightWeight/p:weight /td /tr /xml:group val trainer = new Trainer() val output = trainer.showPeople(xml) () } The User object inherits from MegaProtoUser. The problem is I am not sure how to create a mock user and sign them in. I have tried in my unit test override def setUp : Unit = { val user = User.create user.firstName(XXX) user.lastName(YYY) user.save User.logUserIn(user) } The mock user log-in *seems* to work fine but when User.currentUser.open_! is called it throws an exception on trying to open an empty box. So either how do I do this or how do others do this type of testing. I am sure I am missing something simple. Thanks, ryan -- Lift, the simply functional web framework http://liftweb.net Beginning Scala http://www.apress.com/book/view/1430219890 Follow me: http://twitter.com/dpp Surf the harmonics -- Bill Venners Artima, Inc. http://www.artima.com --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Lift group. To post to this group, send email to liftweb@googlegroups.com To unsubscribe from this group, send email to liftweb+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/liftweb?hl=en -~--~~~~--~~--~--~---
[Lift] Re: Testing snippets that depend on a user logged in
Bill, Thanks for posting this. I am, by experience (I started using it, I can use it enough to write basic tests, I know no more) using Specs. I would welcome and encourage some sample tests in Lift archetypes that use ScalaTest. I want to make sure that folks who pick up Lift get to experience ScalaTest as well as Specs... that way, folks who have a better understanding of tests can make better choices. Thanks, David On Thu, Oct 1, 2009 at 1:27 PM, Bill Venners b...@artima.com wrote: Hi Ryan, It looks like you're currently using a JUnit TestCase. If you want an easier port to something that would work you could use a ScalaTest Suite like this: import org.scalatest.Suite class YourSuite extends Suite { val session = new LiftSession(, randomString(20), Empty) val stableTime = now override def withFixture(test: NoArgTest) { S.initIfUninitted(session) { val user = User.create user.firstName(XXX) user.lastName(YYY) user.save User.logUserIn(user) test() } } def testValue() { val xml = xml:group tr td p:fullNameMy Name/p:fullName /td td p:styleFighter Style/p:style /td td p:weightWeight/p:weight /td /tr /xml:group val trainer = new Trainer() val output = trainer.showPeople(xml) // seems like you need an assertion here... } } A Suite considers methods that start with test as tests, like JUnit 3, except they don't need to result in Unit so you don't need an extra () at the end. The withFixture method is an alternative to beforeEach/afterEach, which are like JUnit 3's setUp/tearDown methods. Each test gets passed as a function to withFixture, which is responsible for executing the test by invoking the function. In this case, it is executed in the context created by S. initIfUninitted. This is ScalaTest 1.0, which is available as a SNAPSHOT right now but should be released proper a week from Monday. http://www.artima.com/scalatest Bill On Thu, Oct 1, 2009 at 9:50 AM, David Pollak feeder.of.the.be...@gmail.com wrote: Using Specs 1.6: object HelloWorldTestSpecs extends Specification { val session = new LiftSession(, randomString(20), Empty) val stableTime = now override def executeExpectations(ex: Examples, t: =Any): Any = { S.initIfUninitted(session) { ... put your User init here. The User.logUserIn will be within the context of a session and thus session (and request) vars will be valid } } HelloWorld Snippet should { Put the time in the node in { ... do testing here } } } Hope this helps. On Thu, Oct 1, 2009 at 8:55 AM, rstradling ryanstradl...@gmail.com wrote: I have a class called class Trainer { def showPeople(xhtml : Group) : NodeSeq = { val user : User = User.currentUser.open_! ... } } I then want to write a unit test to test that returns proper xml. The test is written as so def testValue() = { val xml = xml:group tr td p:fullNameMy Name/p:fullName /td td p:styleFighter Style/p:style /td td p:weightWeight/p:weight /td /tr /xml:group val trainer = new Trainer() val output = trainer.showPeople(xml) () } The User object inherits from MegaProtoUser. The problem is I am not sure how to create a mock user and sign them in. I have tried in my unit test override def setUp : Unit = { val user = User.create user.firstName(XXX) user.lastName(YYY) user.save User.logUserIn(user) } The mock user log-in *seems* to work fine but when User.currentUser.open_! is called it throws an exception on trying to open an empty box. So either how do I do this or how do others do this type of testing. I am sure I am missing something simple. Thanks, ryan -- Lift, the simply functional web framework http://liftweb.net Beginning Scala http://www.apress.com/book/view/1430219890 Follow me: http://twitter.com/dpp Surf the harmonics -- Bill Venners Artima, Inc. http://www.artima.com -- Lift, the simply functional web framework http://liftweb.net Beginning Scala http://www.apress.com/book/view/1430219890 Follow me: http://twitter.com/dpp Surf the harmonics --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Lift group. To post to this group, send email to liftweb@googlegroups.com To unsubscribe from this group, send email to liftweb+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/liftweb?hl=en
[Lift] Re: Testing snippets that depend on a user logged in
Hi David, Thanks. I appreciate that. I was actually already planning to request getting some ScalaTest examples in the Lift archetypes right after ScalaTest 1.0 comes out (on Oct 12, if all goes as planned), and have already arranged with David Bernard to put ScalaTest examples into simple-archetype-simple. I think it is great that we have three decent Scala-specific testing tools already, specs, ScalaTest, and ScalaCheck, plus the trusty Java tools JUnit and TestNG. People have a lot of choice, so it is good that the archetypes would show some of the options. I would also suggest we include a ScalaCheck example in the archetypes as well. I can use ScalaCheck from one of the ScalaTest examples I submit if you like that idea. The downside is that it would add one more dependency, but really I think people should find out about ScalaCheck. Bill On Thu, Oct 1, 2009 at 1:53 PM, David Pollak feeder.of.the.be...@gmail.com wrote: Bill, Thanks for posting this. I am, by experience (I started using it, I can use it enough to write basic tests, I know no more) using Specs. I would welcome and encourage some sample tests in Lift archetypes that use ScalaTest. I want to make sure that folks who pick up Lift get to experience ScalaTest as well as Specs... that way, folks who have a better understanding of tests can make better choices. Thanks, David On Thu, Oct 1, 2009 at 1:27 PM, Bill Venners b...@artima.com wrote: Hi Ryan, It looks like you're currently using a JUnit TestCase. If you want an easier port to something that would work you could use a ScalaTest Suite like this: import org.scalatest.Suite class YourSuite extends Suite { val session = new LiftSession(, randomString(20), Empty) val stableTime = now override def withFixture(test: NoArgTest) { S.initIfUninitted(session) { val user = User.create user.firstName(XXX) user.lastName(YYY) user.save User.logUserIn(user) test() } } def testValue() { val xml = xml:group tr td p:fullNameMy Name/p:fullName /td td p:styleFighter Style/p:style /td td p:weightWeight/p:weight /td /tr /xml:group val trainer = new Trainer() val output = trainer.showPeople(xml) // seems like you need an assertion here... } } A Suite considers methods that start with test as tests, like JUnit 3, except they don't need to result in Unit so you don't need an extra () at the end. The withFixture method is an alternative to beforeEach/afterEach, which are like JUnit 3's setUp/tearDown methods. Each test gets passed as a function to withFixture, which is responsible for executing the test by invoking the function. In this case, it is executed in the context created by S. initIfUninitted. This is ScalaTest 1.0, which is available as a SNAPSHOT right now but should be released proper a week from Monday. http://www.artima.com/scalatest Bill On Thu, Oct 1, 2009 at 9:50 AM, David Pollak feeder.of.the.be...@gmail.com wrote: Using Specs 1.6: object HelloWorldTestSpecs extends Specification { val session = new LiftSession(, randomString(20), Empty) val stableTime = now override def executeExpectations(ex: Examples, t: =Any): Any = { S.initIfUninitted(session) { ... put your User init here. The User.logUserIn will be within the context of a session and thus session (and request) vars will be valid } } HelloWorld Snippet should { Put the time in the node in { ... do testing here } } } Hope this helps. On Thu, Oct 1, 2009 at 8:55 AM, rstradling ryanstradl...@gmail.com wrote: I have a class called class Trainer { def showPeople(xhtml : Group) : NodeSeq = { val user : User = User.currentUser.open_! ... } } I then want to write a unit test to test that returns proper xml. The test is written as so def testValue() = { val xml = xml:group tr td p:fullNameMy Name/p:fullName /td td p:styleFighter Style/p:style /td td p:weightWeight/p:weight /td /tr /xml:group val trainer = new Trainer() val output = trainer.showPeople(xml) () } The User object inherits from MegaProtoUser. The problem is I am not sure how to create a mock user and sign them in. I have tried in my unit test override def setUp : Unit = { val user = User.create user.firstName(XXX) user.lastName(YYY) user.save User.logUserIn(user) } The mock user log-in *seems* to work fine but when User.currentUser.open_! is called it throws an exception on trying to open an empty box. So either how do I do this or
[Lift] Re: Testing snippets that depend on a user logged in
Bill, Cool. If you're going to be at Silicon Valley Code Camp on Saturday, let's talk more about any mechanics. Thanks, David On Thu, Oct 1, 2009 at 2:26 PM, Bill Venners b...@artima.com wrote: Hi David, Thanks. I appreciate that. I was actually already planning to request getting some ScalaTest examples in the Lift archetypes right after ScalaTest 1.0 comes out (on Oct 12, if all goes as planned), and have already arranged with David Bernard to put ScalaTest examples into simple-archetype-simple. I think it is great that we have three decent Scala-specific testing tools already, specs, ScalaTest, and ScalaCheck, plus the trusty Java tools JUnit and TestNG. People have a lot of choice, so it is good that the archetypes would show some of the options. I would also suggest we include a ScalaCheck example in the archetypes as well. I can use ScalaCheck from one of the ScalaTest examples I submit if you like that idea. The downside is that it would add one more dependency, but really I think people should find out about ScalaCheck. Bill On Thu, Oct 1, 2009 at 1:53 PM, David Pollak feeder.of.the.be...@gmail.com wrote: Bill, Thanks for posting this. I am, by experience (I started using it, I can use it enough to write basic tests, I know no more) using Specs. I would welcome and encourage some sample tests in Lift archetypes that use ScalaTest. I want to make sure that folks who pick up Lift get to experience ScalaTest as well as Specs... that way, folks who have a better understanding of tests can make better choices. Thanks, David On Thu, Oct 1, 2009 at 1:27 PM, Bill Venners b...@artima.com wrote: Hi Ryan, It looks like you're currently using a JUnit TestCase. If you want an easier port to something that would work you could use a ScalaTest Suite like this: import org.scalatest.Suite class YourSuite extends Suite { val session = new LiftSession(, randomString(20), Empty) val stableTime = now override def withFixture(test: NoArgTest) { S.initIfUninitted(session) { val user = User.create user.firstName(XXX) user.lastName(YYY) user.save User.logUserIn(user) test() } } def testValue() { val xml = xml:group tr td p:fullNameMy Name/p:fullName /td td p:styleFighter Style/p:style /td td p:weightWeight/p:weight /td /tr /xml:group val trainer = new Trainer() val output = trainer.showPeople(xml) // seems like you need an assertion here... } } A Suite considers methods that start with test as tests, like JUnit 3, except they don't need to result in Unit so you don't need an extra () at the end. The withFixture method is an alternative to beforeEach/afterEach, which are like JUnit 3's setUp/tearDown methods. Each test gets passed as a function to withFixture, which is responsible for executing the test by invoking the function. In this case, it is executed in the context created by S. initIfUninitted. This is ScalaTest 1.0, which is available as a SNAPSHOT right now but should be released proper a week from Monday. http://www.artima.com/scalatest Bill On Thu, Oct 1, 2009 at 9:50 AM, David Pollak feeder.of.the.be...@gmail.com wrote: Using Specs 1.6: object HelloWorldTestSpecs extends Specification { val session = new LiftSession(, randomString(20), Empty) val stableTime = now override def executeExpectations(ex: Examples, t: =Any): Any = { S.initIfUninitted(session) { ... put your User init here. The User.logUserIn will be within the context of a session and thus session (and request) vars will be valid } } HelloWorld Snippet should { Put the time in the node in { ... do testing here } } } Hope this helps. On Thu, Oct 1, 2009 at 8:55 AM, rstradling ryanstradl...@gmail.com wrote: I have a class called class Trainer { def showPeople(xhtml : Group) : NodeSeq = { val user : User = User.currentUser.open_! ... } } I then want to write a unit test to test that returns proper xml. The test is written as so def testValue() = { val xml = xml:group tr td p:fullNameMy Name/p:fullName /td td p:styleFighter Style/p:style /td td p:weightWeight/p:weight /td /tr /xml:group val trainer = new Trainer() val output = trainer.showPeople(xml) () } The User object inherits from MegaProtoUser. The problem is I am not sure how to create a mock user and sign them in. I have
[Lift] Re: Testing snippets that depend on a user logged in
Awesome!!! Thanks guys for the help. It now works. I put a how-to wiki document up on github. For me this was one of those times where my google searches did not seem to turn up anything fruitful, so I thought this how-to would be helpful. If it is not helpful, then no hard feelings if the page is deleted. I just wanted to give back. Wiki page http://wiki.github.com/dpp/liftweb/how-to-unit-test-lift-snippets-with-a-logged-in-user On Oct 1, 4:53 pm, David Pollak feeder.of.the.be...@gmail.com wrote: Bill, Thanks for posting this. I am, by experience (I started using it, I can use it enough to write basic tests, I know no more) using Specs. I would welcome and encourage some sample tests in Lift archetypes that use ScalaTest. I want to make sure that folks who pick up Lift get to experience ScalaTest as well as Specs... that way, folks who have a better understanding of tests can make better choices. Thanks, David On Thu, Oct 1, 2009 at 1:27 PM, Bill Venners b...@artima.com wrote: Hi Ryan, It looks like you're currently using a JUnit TestCase. If you want an easier port to something that would work you could use a ScalaTest Suite like this: import org.scalatest.Suite class YourSuite extends Suite { val session = new LiftSession(, randomString(20), Empty) val stableTime = now override def withFixture(test: NoArgTest) { S.initIfUninitted(session) { val user = User.create user.firstName(XXX) user.lastName(YYY) user.save User.logUserIn(user) test() } } def testValue() { val xml = xml:group tr td p:fullNameMy Name/p:fullName /td td p:styleFighter Style/p:style /td td p:weightWeight/p:weight /td /tr /xml:group val trainer = new Trainer() val output = trainer.showPeople(xml) // seems like you need an assertion here... } } A Suite considers methods that start with test as tests, like JUnit 3, except they don't need to result in Unit so you don't need an extra () at the end. The withFixture method is an alternative to beforeEach/afterEach, which are like JUnit 3's setUp/tearDown methods. Each test gets passed as a function to withFixture, which is responsible for executing the test by invoking the function. In this case, it is executed in the context created by S. initIfUninitted. This is ScalaTest 1.0, which is available as a SNAPSHOT right now but should be released proper a week from Monday. http://www.artima.com/scalatest Bill On Thu, Oct 1, 2009 at 9:50 AM, David Pollak feeder.of.the.be...@gmail.com wrote: Using Specs 1.6: object HelloWorldTestSpecs extends Specification { val session = new LiftSession(, randomString(20), Empty) val stableTime = now override def executeExpectations(ex: Examples, t: =Any): Any = { S.initIfUninitted(session) { ... put your User init here. The User.logUserIn will be within the context of a session and thus session (and request) vars will be valid } } HelloWorld Snippet should { Put the time in the node in { ... do testing here } } } Hope this helps. On Thu, Oct 1, 2009 at 8:55 AM, rstradling ryanstradl...@gmail.com wrote: I have a class called class Trainer { def showPeople(xhtml : Group) : NodeSeq = { val user : User = User.currentUser.open_! ... } } I then want to write a unit test to test that returns proper xml. The test is written as so def testValue() = { val xml = xml:group tr td p:fullNameMy Name/p:fullName /td td p:styleFighter Style/p:style /td td p:weightWeight/p:weight /td /tr /xml:group val trainer = new Trainer() val output = trainer.showPeople(xml) () } The User object inherits from MegaProtoUser. The problem is I am not sure how to create a mock user and sign them in. I have tried in my unit test override def setUp : Unit = { val user = User.create user.firstName(XXX) user.lastName(YYY) user.save User.logUserIn(user) } The mock user log-in *seems* to work fine but when User.currentUser.open_! is called it throws an exception on trying to open an empty box. So either how do I do this or how do others do this type of testing. I am sure I am missing something simple. Thanks, ryan -- Lift, the simply functional web frameworkhttp://liftweb.net Beginning Scalahttp://www.apress.com/book/view/1430219890 Follow me:http://twitter.com/dpp Surf the harmonics -- Bill Venners
[Lift] Re: Testing snippets that depend on a user logged in
On Thu, Oct 1, 2009 at 3:03 PM, rstradling ryanstradl...@gmail.com wrote: Awesome!!! Thanks guys for the help. It now works. I put a how-to wiki document up on github. For me this was one of those times where my google searches did not seem to turn up anything fruitful, so I thought this how-to would be helpful. If it is not helpful, then no hard feelings if the page is deleted. I just wanted to give back. You did *the right thing*. I owe you a beer (or other food or beverage of your choice)! Wiki page http://wiki.github.com/dpp/liftweb/how-to-unit-test-lift-snippets-with-a-logged-in-user On Oct 1, 4:53 pm, David Pollak feeder.of.the.be...@gmail.com wrote: Bill, Thanks for posting this. I am, by experience (I started using it, I can use it enough to write basic tests, I know no more) using Specs. I would welcome and encourage some sample tests in Lift archetypes that use ScalaTest. I want to make sure that folks who pick up Lift get to experience ScalaTest as well as Specs... that way, folks who have a better understanding of tests can make better choices. Thanks, David On Thu, Oct 1, 2009 at 1:27 PM, Bill Venners b...@artima.com wrote: Hi Ryan, It looks like you're currently using a JUnit TestCase. If you want an easier port to something that would work you could use a ScalaTest Suite like this: import org.scalatest.Suite class YourSuite extends Suite { val session = new LiftSession(, randomString(20), Empty) val stableTime = now override def withFixture(test: NoArgTest) { S.initIfUninitted(session) { val user = User.create user.firstName(XXX) user.lastName(YYY) user.save User.logUserIn(user) test() } } def testValue() { val xml = xml:group tr td p:fullNameMy Name/p:fullName /td td p:styleFighter Style/p:style /td td p:weightWeight/p:weight /td /tr /xml:group val trainer = new Trainer() val output = trainer.showPeople(xml) // seems like you need an assertion here... } } A Suite considers methods that start with test as tests, like JUnit 3, except they don't need to result in Unit so you don't need an extra () at the end. The withFixture method is an alternative to beforeEach/afterEach, which are like JUnit 3's setUp/tearDown methods. Each test gets passed as a function to withFixture, which is responsible for executing the test by invoking the function. In this case, it is executed in the context created by S. initIfUninitted. This is ScalaTest 1.0, which is available as a SNAPSHOT right now but should be released proper a week from Monday. http://www.artima.com/scalatest Bill On Thu, Oct 1, 2009 at 9:50 AM, David Pollak feeder.of.the.be...@gmail.com wrote: Using Specs 1.6: object HelloWorldTestSpecs extends Specification { val session = new LiftSession(, randomString(20), Empty) val stableTime = now override def executeExpectations(ex: Examples, t: =Any): Any = { S.initIfUninitted(session) { ... put your User init here. The User.logUserIn will be within the context of a session and thus session (and request) vars will be valid } } HelloWorld Snippet should { Put the time in the node in { ... do testing here } } } Hope this helps. On Thu, Oct 1, 2009 at 8:55 AM, rstradling ryanstradl...@gmail.com wrote: I have a class called class Trainer { def showPeople(xhtml : Group) : NodeSeq = { val user : User = User.currentUser.open_! ... } } I then want to write a unit test to test that returns proper xml. The test is written as so def testValue() = { val xml = xml:group tr td p:fullNameMy Name/p:fullName /td td p:styleFighter Style/p:style /td td p:weightWeight/p:weight /td /tr /xml:group val trainer = new Trainer() val output = trainer.showPeople(xml) () } The User object inherits from MegaProtoUser. The problem is I am not sure how to create a mock user and sign them in. I have tried in my unit test override def setUp : Unit = { val user = User.create user.firstName(XXX) user.lastName(YYY) user.save User.logUserIn(user) } The mock user log-in *seems* to work fine but when User.currentUser.open_! is called it throws an exception on trying to open an empty box. So either how do
[Lift] Re: Testing snippets that depend on a user logged in
Hi, I am working at the moment on a small lift demo app and I've added some enhancements to specs in order to ease the testing inside a session. To use those functionalities, you need specs-1.6.1-SNAPSHOT (http:// www.scala-tools.org/repo-snapshots/org/scala-tools/testing/specs/1.6.1-SNAPSHOT). Here are the specification and traits that I use to test the JPA requests to save a User in the database: // First I create Mocks for the lift session import javax.servlet.http._ import net.liftweb.http.{ S, Req, LiftSession } import org.specs.mock.Mockito trait MockRequest extends Mockito { this: Specification = var request = mock[Req] var httpRequest = mock[HttpServletRequest] var session = mock[LiftSession] def createMocks: Unit = { request = mock[Req] httpRequest = mock[HttpServletRequest] session = mock[LiftSession] request.request returns httpRequest } // this method can be used to executed any action inside a mocked session def inSession(f: =Any) { S.init(request, session) { f } } def unsetParameter(name: String) { request.param(name) returns None } def setParameter(name: String, value: String) { request.param (name) returns Some(value) } } // Context creation for the specification // // This Specification context specifies the User table must be cleaned up before each example. // It also makes sure that the example expectations are executed in a mocked session // see http://code.google.com/p/specs/wiki/DeclareSpecifications#Specification_context_(_from_1.6.1_) for more information object DatabaseContext extends Specification with Contexts with MockRequest { val setup = new SpecContext { beforeExample(inSession(Users.createQuery(delete User).executeUpdate)) // delete the User table before each example aroundExpectations(inSession(_)) // execute each example inside a mocked session } } // and finally the specification itself import org.specs._ import org.specs.specification._ class UserSpec extends SpecificationWithJUnit with MockRequest with Contexts { DatabaseContext.setup(this) // set the specification context on this specification A Users repository can { create a user in { val eric = User(etorreborre, password, Eric) Users.mergeAndFlush(eric) // the Users object is a LocalEMF with RequestVarEM so it needs a session Users.find(classOf[User], etorreborre) must_== Some(eric) } } A Users repository should { throw an exception if the user name has a length 5 in { Users.merge(User(e, password, Eric)) must throwAn [Exception] } } } I hope this helps too, please ask if you have any questions. Eric. --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Lift group. To post to this group, send email to liftweb@googlegroups.com To unsubscribe from this group, send email to liftweb+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/liftweb?hl=en -~--~~~~--~~--~--~---
[Lift] Re: Testing snippets that depend on a user logged in
Hi Ryan, David, Eric, I added a ScalaTest version to your wiki page: http://wiki.github.com/dpp/liftweb/how-to-unit-test-lift-snippets-with-a-logged-in-user Eric you may want to add a specs version. Bill On Thu, Oct 1, 2009 at 3:03 PM, rstradling ryanstradl...@gmail.com wrote: Awesome!!! Thanks guys for the help. It now works. I put a how-to wiki document up on github. For me this was one of those times where my google searches did not seem to turn up anything fruitful, so I thought this how-to would be helpful. If it is not helpful, then no hard feelings if the page is deleted. I just wanted to give back. Wiki page http://wiki.github.com/dpp/liftweb/how-to-unit-test-lift-snippets-with-a-logged-in-user On Oct 1, 4:53 pm, David Pollak feeder.of.the.be...@gmail.com wrote: Bill, Thanks for posting this. I am, by experience (I started using it, I can use it enough to write basic tests, I know no more) using Specs. I would welcome and encourage some sample tests in Lift archetypes that use ScalaTest. I want to make sure that folks who pick up Lift get to experience ScalaTest as well as Specs... that way, folks who have a better understanding of tests can make better choices. Thanks, David On Thu, Oct 1, 2009 at 1:27 PM, Bill Venners b...@artima.com wrote: Hi Ryan, It looks like you're currently using a JUnit TestCase. If you want an easier port to something that would work you could use a ScalaTest Suite like this: import org.scalatest.Suite class YourSuite extends Suite { val session = new LiftSession(, randomString(20), Empty) val stableTime = now override def withFixture(test: NoArgTest) { S.initIfUninitted(session) { val user = User.create user.firstName(XXX) user.lastName(YYY) user.save User.logUserIn(user) test() } } def testValue() { val xml = xml:group tr td p:fullNameMy Name/p:fullName /td td p:styleFighter Style/p:style /td td p:weightWeight/p:weight /td /tr /xml:group val trainer = new Trainer() val output = trainer.showPeople(xml) // seems like you need an assertion here... } } A Suite considers methods that start with test as tests, like JUnit 3, except they don't need to result in Unit so you don't need an extra () at the end. The withFixture method is an alternative to beforeEach/afterEach, which are like JUnit 3's setUp/tearDown methods. Each test gets passed as a function to withFixture, which is responsible for executing the test by invoking the function. In this case, it is executed in the context created by S. initIfUninitted. This is ScalaTest 1.0, which is available as a SNAPSHOT right now but should be released proper a week from Monday. http://www.artima.com/scalatest Bill On Thu, Oct 1, 2009 at 9:50 AM, David Pollak feeder.of.the.be...@gmail.com wrote: Using Specs 1.6: object HelloWorldTestSpecs extends Specification { val session = new LiftSession(, randomString(20), Empty) val stableTime = now override def executeExpectations(ex: Examples, t: =Any): Any = { S.initIfUninitted(session) { ... put your User init here. The User.logUserIn will be within the context of a session and thus session (and request) vars will be valid } } HelloWorld Snippet should { Put the time in the node in { ... do testing here } } } Hope this helps. On Thu, Oct 1, 2009 at 8:55 AM, rstradling ryanstradl...@gmail.com wrote: I have a class called class Trainer { def showPeople(xhtml : Group) : NodeSeq = { val user : User = User.currentUser.open_! ... } } I then want to write a unit test to test that returns proper xml. The test is written as so def testValue() = { val xml = xml:group tr td p:fullNameMy Name/p:fullName /td td p:styleFighter Style/p:style /td td p:weightWeight/p:weight /td /tr /xml:group val trainer = new Trainer() val output = trainer.showPeople(xml) () } The User object inherits from MegaProtoUser. The problem is I am not sure how to create a mock user and sign them in. I have tried in my unit test override def setUp : Unit = { val user = User.create user.firstName(XXX) user.lastName(YYY) user.save User.logUserIn(user) } The mock user log-in *seems* to work fine but when User.currentUser.open_! is called it throws an exception on trying to open an empty box. So either how do I do this or how do others do this type