2016-06-28 2 views
1

저는 scalat와 scalamock을 처음 사용하지만 몇 가지 테스트를 작성했습니다. 그러나, 내 테스트에서 선물을 사용하면 (결과를 조롱하고 클래스 자체에서) 테스트가 통과하지 않는 것 같습니다.스칼라 테스터와 스칼라톡, 선물이 관련되어있을 때 테스트가 통과하지 않습니다.

github에서 호스팅되는 최소한의 작업 예제를 작성했습니다. "마스터"는 선물을 포함하고 "withNoFutures"는 그렇지 않습니다.

나는 여러 분기에서 테스트를 여러 번 실행했으며 "마스터"의 테스트는 가끔씩 통과하지만 "withNoFutures"의 테스트는 항상 통과합니다. 선물을 가지고 시험을 치도록 누군가도 도와 줄 수 있습니까?

package example 

import org.scalamock.scalatest.MockFactory 
import org.scalatest.{BeforeAndAfter, FunSuite} 

import scala.concurrent.Future 


class SomeServiceTest extends FunSuite with BeforeAndAfter with MockFactory { 

    var otherService: SomeOtherService = _ 

    var service: SomeService = _ 

    before { 
    otherService = mock[SomeOtherService] 
    service = mock[SomeService] 
    } 

    after { 
    otherService = null 
    service = null 
    } 

    test("a first test works") { 
    otherService.execute _ expects() once() 
    service.execute _ expects(*) returning Future.successful(true) once() 
    SomeService.execute(service, otherService) 
    } 


    // Why this test does not work? 
    test("a second test works") { 
    // Copy paste the code in the first test 
    } 

} 

내가 (해당 구현 클래스를 변경) 만 부울을 반환하는 코드를 변경하는 경우, 모두 잘 작동 :

Github에서의 환매 특약은 : 같은 https://github.com/vicaba/scalatestNotWorking/tree/master

실패한 테스트 코드가 보인다. 그렇지 않으면 결과는 그렇게 할 수있는 한 가지 방법은 당신의 코드를 실행 한 후 결과 나 준비 (기능이 실패 할 경우) 대기하는 것입니다

답변

1

은 (/ shouldBe/등을 주장한다.) 결정적 (undeterministic)입니다

import scala.concurrent.Await 
import scala.concurrent.duration._ 

//res0 is of type Future[T] 
val res0 = Await.ready(firstFeature, FiniteDuration(1,SECONDS)) 
//res1 is of type T (from Future[T]) 
val res1 = Await.result(secondFeature, FiniteDuration(1,SECONDS)) 

또 다른 방법 ScalaTest 2.0에서 ScalaFuture를 사용하는 것입니다. here

관련 문제