Testing
Testability is very important
Skinny Framework puts emphasis on testable application development and provides toolkit for testing.
How to run tests
Simply use skinny command or sbt directly.
# run all the tests
./skinny test
# run single test
./skinny testOnly model.MemberSpec
# run only failed tests
./skinny testQuick
# run multiple tests under controller package when changes are detected
./skinny ~testOnly controller.*Spec
ORM Tests
Here is some examples:
https://github.com/skinny-framework/skinny-framework/tree/master/orm/src/test/scala/blog
package model
import org.joda.time._
import org.scalatest._
import org.scalatest.fixture.FunSpec
import skinny._
import skinny.test._
import scalikejdbc._
import scalikejdbc.scalatest._
class MemberSpec extends FunSpec with AutoRollback with Matchers with DBSettings {
override def fixture(implicit session: DBSession) {
// do fixtures stuff
}
describe("Member") {
it("should find all entities") { implicit session =>
Member.findAll().size should be >(0)
}
}
}
Mocked Controller Tests
Skinny provides MockController
trait for light-weight controller tests.
package controller
class RootController extends ApplicationController {
def index = render("/root/index")
}
import org.scalatest._
import skinny.DBSettings
import skinny.test.MockController
class RootControllerSpec extends FunSpec with Matchers with DBSettings {
describe("RootController") {
it("shows top page") {
val controller = new RootController with MockController
controller.index
controller.contentType should equal("text/html; charset=utf-8")
controller.renderCall.map(_.path) should equal(Some("/root/index"))
}
}
}
Be aware of following points when you use MockController
:
MockController cannot handle skinny.micro.control.HaltException
HaltException is very exceptional operation by Skinny Micro. HaltException is not an Exception but a Throwable. Furthermore, it mutes all the stack traces.
When you call halt
method or redirect
method, HaltException will be thrown.
We recommend you using redirect302
instead because it is excellent with MockController
testing.
Integration Tests
Skinny Micro offers a great test toolkit with embedded Jetty server and easy-to-use DSL. Of course, you can use them without any problem.
Below is an integration test example with scalatra-test.
package integrationtest
import org.skinny.test._
import skinny.DBSettings
import skinny.test.SkinnyTestSupport
import _root_.controller.Controllers
import org.scalatest._
class IntegrationTestSpec extends SkinnyFlatSpec with Matchers with SkinnyTestSupport with DBSettings {
addFilter(Controllers.members, "/*")
it should "show index page" in {
withSession("userId" -> "Alice") {
get("/members") {
status should equal(200)
body should contain("something")
}
}
}
}
Coverage Report
If you need a code coverage report, use coverage test
instead.
https://github.com/scoverage/sbt-scoverage
./skinny test:coverage
More Examples
It’s the fastest way to learn is seeing generated tests by scaffold command.
And also some examples here may be helpful for you:
See also: FactoryGirl