Can I do this with Spock?

I saw an example in ROR to test some domain class:

context "Validations" do
   [:city, :zip, :street].each do |attr|
      it "must have a #{attr}" do
         a = Address.new
         a.should_not be_valid
         a.errors.on(attr).should_not be_nil
      end
   end
end

It creates tests on the fly with different meanings of different names ... This is pretty interesting, but ... can I do this with spock or jUnit?

Thank you so much

+3
source share
1 answer

Using Spock:

class Validations extends Specification {
    def "must have a #attr"() {
        def a = new Address()

        expect:
        !a.valid
        a.errors.on(attr) != null

        where:
        attr << ["city", "zip", "street"]
    }
}

If there is more than one data variable, the table syntax is more convenient:

        ...
        where:
        attr1    | attr2
        "city"   | ...
        "zip"    | ...
        "street" | ...
+6
source

All Articles