Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is shoulda destroying my backtraces?

I have a test more or less like this:

class FormDefinitionTest < ActiveSupport::TestCase
  context "a form_definition" do
    setup do
      @definition = SeedData.form_definition
      # ...

I've purposely added a

raise "blah"

somewhere down the road and I get this error:

RuntimeError: blah
test/unit/form_definition_test.rb:79:in `__bind_1290079321_362430'

when I should be getting something along:

/Users/pupeno/projectx/db/seed/sheet_definitions.rb:17:in `sheet_definition': blah (RuntimeError)
    from /Users/pupeno/projectx/db/seed/form_definitions.rb:4:in `form_definition'
    from /Users/pupeno/projectx/test/unit/form_definition_test.rb:79

Any ideas what is sanitizing/destroying my backtraces? My suspicious is shoulda because the when the exception happens inside a setup or should is whet it happens.

This is a Rails 3 project, in case that's important.

like image 981
pupeno Avatar asked Nov 18 '10 11:11

pupeno


1 Answers

That is because the shoulda method #context is generating code for you. for each #should block it generates a completely separate test for you so e.g.

class FormDefinitionTest < ActiveSupport::TestCase
  context "a form_definition" do
    setup do
      @definition = SeedData.form_definition
    end

    should "verify some condition" do
      assert something
    end

    should "verify some other condition" do
      assert something_else
    end
  end
end

Then #should will generate two completely independent tests (for the two invocations of #should), one that executes

      @definition = SeedData.form_definition
      assert something

and another one that executes

      @definition = SeedData.form_definition
      assert something_else

It is worth noting that it does not generate one single test executing all three steps in a sequence.

These generated blocks of codes have method names like _bind_ something and the generated test have name that is a concatenation of all names of the contexts traversed to the should block plus the string provided by the should block (prefixed with "should "). There is another example in the documentation for shoulda-context.

like image 63
Jarl Avatar answered Nov 07 '22 08:11

Jarl