Mocking game! and scala

I have a mockery issue in the Play app. I have an application as follows:

object Application extends Controller {
    def login = Action {implicit request =>
        val email = ... //Some email from the request
        if(EmailChecker.checkEmail(email)) {
            Ok("Email is checked and is fine")
        } else {
            Ok("Email is wrong")
        }
    }
}

What I want to do is test the request, but make fun of EmailChecker because it is looking for some search in some database, and that is not what I want to do in my test.

I have seen several lessons on how to scoff at Scala, but I can not find anything that covers the case that I have.

Any help / pointers / tutorials that show how to do what I want to do would be great.

I am new to the Games! and Scala ...

+5
source share
1 answer

One possible solution:

class Application(emailChecker: EmailChecker) extends Controller {
    def login = Action {implicit request =>
        val email = ... //Some email from the request
        if(emailChecker.checkEmail(email)) {
            Ok("Email is checked and is fine")
        } else {
            Ok("Email is wrong")
        }
    }
}

object Application extends Application(EmailChecker)

And the test will be:

import org.specs2.Specification
import org.specs2.mock.Mockito

class ApplicationUnitSpec extends Specification with Mockito { def is = 
    "Test Application" ! {
        val emailChecker = mock[EmailChecker]
        val response = new Application(emailChecker).login(FakeRequest)
        there was one(emailChecker).checkEmail("blah@example.com")
    }
}

, Real Test implicits, , , , EmailChecker, , . emailChecker . .

+5

All Articles