Tag Archives: cucumber

Idea for a concise syntax of nested models in cucumber

I started thinking about how to more easily specify some of the deeply nested structures we need during testing.

First we already have a step for doing this.  An example which looks like this:

And the following investigations with multiple choices exist:
| investigation         | activity    | section     | page       | multiple_choices  | image_questions   |
| first investigation   | act 1     | section 1  | page 1   | a, b              |                   |
| first investigation   | act 2     | section 2  | page 2   | c, d              |                   |
| second investigation  | act 3     | section 3  | page 3   | b_a, b_b          | image_q           |
| second investigation  | act 4     | section 4  | page 4   | b_c, b_d          |                   |

That is using some previously defined multiple_choice and image_question objects. I was trying to see if I could write it more like this:

And the following investigations with multiple choices exist:
      investigation "first investigation"
        activity "act 1"
          section "section 1"
            page "page 1"
              multiple_choice :prompt => "Prompt 1", :choices => "a,b,c,d", :correct_answer => "a"
              multiple_choice :prompt => "Prompt 2", :choices => "a,b,c,d", :correct_answer => "a"
            page "page 2"
              image_question :prompt => "Image Prompt"

I didn’t see an easy way to do that but with a little method_missing, haml, and using cucumber’s “”” notation, the following looks pretty straight forward:

And the following investigations with multiple choices exist:
      """
      - investigation "first investigation" do
        - activity "act 1" do
          - section "section 1" do
            - page "page 1" do
              - multiple_choice :prompt => "Prompt 1", :choices => "a,b,c,d", :correct_answer => "a" do
              - multiple_choice :prompt => "Prompt 2", :choices => "a,b,c,d", :correct_answer => "a" do
            - page "page 2" do
              - image_question :prompt => "Image Prompt" do
      """

In a simple implementation a map could be used to define the code to add a child object. So for investigation it would be {activities<<child}, and for page it is {add_element(child)}. The creation of of the objects could use factory girl so the names could be any of the existing factories.

I’m curious if anyone has seen something like this that is already written? And I’m wondering if there is something like haml that uses indentation for blocks but defaults to “ruby mode” so there wouldn’t be a need for the “- ” before each line.