2016-07-30 3 views
2

私は自分の俳優の単体テストを書こうとしており、基本的な嘲笑に固執しています。 PriceAggregateActorはakkaの永続性を使用しています。私はそれをすべてのconfに渡したくないので、完全にそれを嘲笑したいと思います。私はいつもそうだAkkaで子どもの俳優を嘲笑

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がテストの子アクターであるためです。見つからないコンストラクタ引数は、テストへの参照(親クラス)です。

あなたは3つのオプションがあります。

  1. MockedChildトップレベルのクラス(またはオブジェクトのメンバー)してくださいProps
  2. の名前付きパラメータの形式を使用し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イアン:) – Reeebuuk

関連する問題