These are chat archives for etorreborre/specs2

21st
Nov 2018
noanativ
@noanativ
Nov 21 2018 15:25
We are trying to create fragments dynamically inside a method that is called from a regular should/in block.
The method is defined on a class, FragmentsFactoryIT bellow, which extends SpecificationWithJunit.
When creating fragments outside, inside the should/in block it looks good and we get the expected result (see "koo" example).
But inside the function it does not ("boo" example) and also outside but in Scope ("foo" example).
We see a single test description in console and "Empty test suite" message.
Any idea why and how we can overcome this?
image.png
image.png
Eric Torreborre
@etorreborre
Nov 21 2018 15:30
That’s the beauty of mutation :-(. Is it a spec where you want to generate everything or just some examples. Because if it is everything you might as well use the “immutable” style
Eric Torreborre
@etorreborre
Nov 21 2018 15:44
One first answer. Scope is supposed to be used to create a example single example not a bunch of them so this case will not work. I need to understand why you are trying to use Scope there to propose some alternatives
Eric Torreborre
@etorreborre
Nov 21 2018 15:51

I think I understand what is wrong. When you create fragments is a mutable spec they end up “attached” to the specification. Here the factory is “attaching” them to a different specification. So using a trait instead of a separate class will work

trait MyFragmentsFactory extends org.specs2.mutable.SpecificationWithJUnit {
  def generateFragments =
    Fragments.foreach(1 to 3) { i =>
      "David "+ i ! { i === i } ^ br
    }
}

class MyFragmentsSpec extends org.specs2.mutable.SpecificationWithJUnit with MyFragmentsFactory {
  // val myFactory = new MyFragmentsFactory

  "my factory" should {
    "foo" in new org.specs2.specification.Scope {
      Fragments.foreach(1 to 3) { i => "David "+ i ! { i === i } ^ br }
    }

    "koo" in  {
      Fragments.foreach(1 to 3) { i => "David "+ i ! { i === i } ^ br }
    }

    "boo" >> generateFragments
  }
}

(Except in the Scope case as I wrote before)

noanativ
@noanativ
Nov 21 2018 16:11
The trait worked for me!
Actually, even just extending from class MyFragmentsFactory, will "generate" only one specification
image.png
noanativ
@noanativ
Nov 21 2018 16:17
If i'll use addFragment(generateFragments()) inside "boo"? do you think it might work?
Eric Torreborre
@etorreborre
Nov 21 2018 16:28
The version I posted works on in sbt, I need to test it with junit
image.png
looks good in IntelliJ too