#3 Refactored tests

This commit is contained in:
Eugene Zadyra
2018-06-04 13:27:28 +02:00
parent a8fcd1ecbe
commit 6edf5a284b
3 changed files with 30 additions and 19 deletions

View File

@@ -0,0 +1,11 @@
package com.lbs.server.actor
import akka.actor.ActorSystem
import akka.testkit.{ImplicitSender, TestKit}
import org.scalatest.{BeforeAndAfterAll, WordSpecLike}
abstract class AkkaTestKit extends TestKit(ActorSystem()) with ImplicitSender with WordSpecLike with BeforeAndAfterAll {
override def afterAll(): Unit = {
TestKit.shutdownActorSystem(system)
}
}

View File

@@ -1,30 +1,18 @@
package com.lbs.server.actor
import akka.actor.{ActorRef, ActorSystem}
import akka.testkit.{ImplicitSender, TestKit, TestProbe}
import com.lbs.bot.model.{Command, Message, MessageSource, TelegramMessageSourceSystem}
import com.lbs.server.actor.Chat.Init
import com.lbs.server.actor.Login.{ForwardCommand, LoggedIn, UserId}
import com.lbs.server.service.DataService
import org.mockito.Mockito._
import org.scalatest.{BeforeAndAfterAll, WordSpecLike}
class AuthSpec extends TestKit(ActorSystem()) with ImplicitSender with WordSpecLike with BeforeAndAfterAll {
override def afterAll(): Unit = {
TestKit.shutdownActorSystem(system)
}
class AuthSpec extends AkkaTestKit with Mocks {
"An Auth actor " when {
val source = MessageSource(TelegramMessageSourceSystem, "1")
val userId = UserId(1L, source)
"user is unauthorized" must {
val unauthorizedHelpActor = TestProbe()
val loginActor = TestProbe()
val chatActor = TestProbe()
val unauthorizedHelpFactory: MessageSource => ActorRef = _ => unauthorizedHelpActor.ref
val loginActorFactory: (MessageSource, ActorRef) => ActorRef = (_, _) => loginActor.ref
val chatActorFactory: UserId => ActorRef = _ => chatActor.ref
val dataService = mock(classOf[DataService])
val auth = system.actorOf(Auth.props(source, dataService, unauthorizedHelpFactory, loginActorFactory, chatActorFactory))
@@ -75,12 +63,6 @@ class AuthSpec extends TestKit(ActorSystem()) with ImplicitSender with WordSpecL
}
"user is authorized" must {
val unauthorizedHelpActor = TestProbe()
val loginActor = TestProbe()
val chatActor = TestProbe()
val unauthorizedHelpFactory: MessageSource => ActorRef = _ => unauthorizedHelpActor.ref
val loginActorFactory: (MessageSource, ActorRef) => ActorRef = (_, _) => loginActor.ref
val chatActorFactory: UserId => ActorRef = _ => chatActor.ref
val dataService = mock(classOf[DataService])
val auth = system.actorOf(Auth.props(source, dataService, unauthorizedHelpFactory, loginActorFactory, chatActorFactory))

View File

@@ -0,0 +1,18 @@
package com.lbs.server.actor
import akka.actor.{ActorRef, ActorSystem}
import akka.testkit.TestProbe
import com.lbs.bot.model.MessageSource
import com.lbs.server.actor.Login.UserId
trait Mocks {
protected implicit val system: ActorSystem
protected val unauthorizedHelpActor = TestProbe()
protected val loginActor = TestProbe()
protected val chatActor = TestProbe()
protected val unauthorizedHelpFactory: MessageSource => ActorRef = _ => unauthorizedHelpActor.ref
protected val loginActorFactory: (MessageSource, ActorRef) => ActorRef = (_, _) => loginActor.ref
protected val chatActorFactory: UserId => ActorRef = _ => chatActor.ref
}