2017-04-20 18 views
0

Lists의 외부 Scalacheck 생성기와 적절한 유형을 사용하여 Gen.oneOf(List[T])을 사용했습니다. 빈 값에 대한 자리 표시자를 반환하는 것이 때때로 유용 할 것이라고 생각합니다. 현재 목록이 채워집니다. 이 문제를 어떻게 해결해야합니까? 목록 끝에 빈 형식을 추가하려고합니까? 그렇다면 어떻게해야합니까? 그렇지 않다면 어떻게하면 제네레이터에 빈 값을 추가 할 수 있습니까? 그것은 간단하지만, 지금 당장 문제를 파악하는 데 어려움을 겪고 있습니다.Scalatest에서 속성 검사가 실패합니다

import org.scalatest.FlatSpec 
import org.scalacheck.Gen 
import org.scalacheck.Prop.exists 
import org.scalatest.prop.PropertyChecks 

class EventFieldGeneratorTest extends FlatSpec with PropertyChecks { 
    behavior of "Gen.option" 
    it should "occasionally return None" in { 
    val colors = Gen.oneOf("Blue", "Red", "Green", "Yellow") 
    val opt = Gen.option(colors) 
    val list = Gen.listOfN(20, opt) 
    val p1 = exists(list)(_ == None) 
    p1.check 
    } 
} 

아무도 내 테스트가 포기하는 이유를 설명 할 수 있습니까?

Testing started at 10:31 AM ... ! Gave up after only 0 passed tests. 501 tests were discarded. 

Process finished with exit code 0 

어떻게 ScalaTest에서 실패한 결과로 표시 할 수 있습니까? Flatspec을 사용하는 것이 좋지 않습니까?

은 어쩌면 내가

는 여기에 내가 그것을 정렬하는 데 사용되는 문서의 ... check 이외의 것을 사용하여야한다. 내가 옵션 값 목록을 사용하려고에 결함이 아무것도 없다고 생각

http://www.scalatest.org/user_guide/writing_scalacheck_style_properties

답변

1

다음 Scalatest 페이지에서. 당신에게 문제가되는 몇 가지 문제가 있습니다.

Scalatest 프레임 워크를 사용하는 경우 Scalatest 인프라를 사용하여 Scalacheck를 사용해야합니다. 따라서 Scalatest matchers를 사용해야하고, Scalatest 맛의 속성 (forAll 사용)을 작성해야하지만 Scalacheck의 생성자는 직접 사용합니다.

목록 간의 유형 유추와 Option 유형이 문제가됩니다. 당신이 shouldBe 정규를 사용하는 경우,

x shouldBe(None) 

당신은 Scalatest에서 관련 런타임 오류가 발생합니다 : 당신은 Option 유형 목록이 일치하지 않아야

[info] - should occasionally return None *** FAILED *** 
[info] TestFailedException was thrown during property evaluation. 
[info]  Message: List() was not equal to None 
[info]  Location: (GenTest.scala:13) 
[info]  Occurred when passed generated values (
[info]  arg0 = List() // 5 shrinks 
[info] ) 
[info] Run completed in 1 second, 621 milliseconds. 
[info] Total number of tests run: 1 
[info] Suites: completed 1, aborted 0 
[info] Tests: succeeded 0, failed 1, canceled 0, ignored 0, pending 0 

. 당신은 Scalatest "용기"정규와 일치해야 should contain

import org.scalatest.FlatSpec 
import org.scalatest.Matchers 
import org.scalacheck.Gen 
import org.scalatest.prop.PropertyChecks 

class EventFieldGeneratorTest extends FlatSpec with Matchers with PropertyChecks { 
    behavior of "Gen.option" 
    it should "occasionally return None" in { 
    val colors = Gen.oneOf("Blue","Red","Green","Yellow") 
    val opt = Gen.option(colors) 
    val list = Gen.listOfN(20,opt) 
    forAll(list) { (xs: List[Option[String]]) => 
     xs should contain (None) 
    } 
    } 
} 

이 당신에게 성공 속성 검사 제공 :

Scalatest에
[info] EventFieldGeneratorTest: 
[info] Gen.option 
[info] - should occasionally return None 
[info] ScalaTest 
[info] Run completed in 1 second, 9 milliseconds. 
[info] Total number of tests run: 1 
[info] Suites: completed 1, aborted 0 
[info] Tests: succeeded 1, failed 0, canceled 0, ignored 0, pending 0 
[info] All tests passed. 
[info] Passed: Total 1, Failed 0, Errors 0, Passed 1 

더는

http://www.scalatest.org/user_guide/using_matchers

를 정합을