views:

50

answers:

2

We have a workflow need to be tested. So there are so many branches (pathes) need to test Now we using shoulda contextual test

like this:

context "workflow one" do 
  setup do 
    #do something
  end

  context "branch 1 succuess"  do
    should "something" do 
    end

    context "branch 2 succuess"  do
      should "something" do 
      end

      context ...
    end
  end

  context "branch 1 fail" do
    should "something" do 
    end

    context ...
  end
end

You know it will be very messy. Do you have good solution?

A: 

I'd generally split them up on a "per-branch" basis. This assumes that when a branch "fails" it basically just ends the process (and you can't go on to the other branches). At least this way you can group your tests. EG:

context "workflow one" do 
setup do 
  #do something
end

  context "Testing branch 1" do 
    context "branch 1 succuess"  do
      should "something" do 
      end

    context "branch 1 fail" do
      should "something" do 
      end
    end
  end

  # assumes branch one is successful 
  context "Testing branch 2" do
    setup do
      do_branch_one_success
    end 
    context "branch 2 succuess"  do
      should "something" do 
      end

    context "branch 2 fail" do
      should "something" do 
      end
    end
  end
...

end
Taryn East
A: 

Maybe you can simply structure your code differently??

If you think about validations in ActiveRecord, lots of different branches (invalid input) are turned into a list. Then each validation can be tested in isolation.

So, in your case, can you not turn your workflow into a list of actions to be performed??

Stephan

Stephan Wehner