2016-07-30 2 views
2

나는 배우를위한 단위 테스트를 쓰려고하는데 기본적인 조롱에 집착하고있다. PriceAggregateActor는 akka 지속성을 사용하고 있으며 모든 conf를 전달하지 않으려 고 완전히 모의하고 싶습니다. 난 항상 받고 있어요아카의 조롱 배우

class CommandPriceActorTest extends TestKit(ActorSystem("test-benefits", 
    ConfigFactory.parseString("""akka.loggers = ["akka.testkit.TestEventListener"] """))) with FlatSpecLike with Matchers 
    with BeforeAndAfterAll with Eventually{ 

    class MockedChild extends Actor { 
    def receive = { 
     case _ => lala 
    } 
    } 

    val probe = TestProbe() 
    val commandPriceActor = TestActorRef(new CommandPriceActor(Props[MockedChild])) 

:

Caused by: java.lang.IllegalArgumentException: no matching constructor found on class CommandPriceActorTest$MockedChild for arguments [] 

내가 뭔가를 할 노력하고있어 내 테스트에서 그래서

object CommandPriceActor { 
    def apply() = Props(classOf[CommandPriceActor], PriceAggregateActor()) 
} 

class CommandPriceActor(priceAggregateActorProps: Props) extends Actor with ActorLogging { 

    val priceAggregateActor = context.actorOf(priceAggregateActorProps, "priceAggregateActor") 

을 테스트 할 배우

왜 mockedChild에 대해 불평합니까? 생성자 인수를 가져서는 안됩니다.

답변

1

MockedChild가 테스트의 하위 액터이기 때문입니다. 누락 된 생성자 인수는 테스트 (부모 클래스)에 대한 참조입니다.

당신은 세 가지 옵션이 있습니다

  1. Props
  2. 사용 MockedChild Props
  3. 만들기의 명명 된 매개 변수 양식 (객체 또는 회원) 최상위 클래스로 this에 대한 참조를 전달을

옵션 1

val probe = TestProbe() 
val mockProps = Props(classOf[MockedChild], this) 
val commandPriceActor = TestActorRef(new CommandPriceActor(mockProps)) 

옵션 2

val probe = TestProbe() 
val mockProps = Props(new MockedChild) 
val commandPriceActor = TestActorRef(new CommandPriceActor(mockProps)) 

옵션 3

val probe = TestProbe() 
val mockProps = Props(new CommandPriceActorTest.MockedChild) 
val commandPriceActor = TestActorRef(new CommandPriceActor(mockProps)) 

// .... 

object CommandPriceActorTest { 
    class MockedChild extends Actor { 
    def receive = { 
     case _ => lala 
    } 
    } 
} 
+0

난 당신이 무슨 말을하는지 얻을하지만 코드에서 것을 어떻게 작성합니까? :) – Reeebuuk

+0

몇 가지 예를 추가했습니다. 원래 대답은 내 전화로 공항에서 이루어졌습니다. – iain

+0

매력처럼 작동합니다! Thx Ian :) – Reeebuuk