How to mock a Scala singleton object?

后端 未结 4 415
我在风中等你
我在风中等你 2020-12-09 03:53

I am trying to mock a Scala singleton object. In particular, I need to mock the object play.api.libs.ws.WS used inside a service component (class under test). U

4条回答
  •  感情败类
    2020-12-09 04:39

    I don't like to change the design to accommodate this kind of limitation. basically what I can get is all to change the design and then use the mock framework

      //since mocking frameworks on SCALA is not support mocking singleton
      //hacks are either required to change the design or ...
      //I'd rather putting the mocking logic here by myself
      var testUser:Option[User] = None;
    
      def getCurrentUser(request: Request[Object]) = {
        if( testUser != None ){
        testUser;
      }
    

    hope it helps.

提交回复
热议问题