vendredi 22 mai 2015

MiniTest fails but UnitTest works for inheritance

I have a code in Rails v2.3 as follows with MiniTest v4.7.5:

Class

class Pdf
 def generator_class
   return GeneratePDF::Image
 end
end

Module

module GeneratePDF
 class Image
   # ...
 end
end

Testcase

require_relative '../test_helper'class PdfTest < ActiveSupport::TestCase
 should "return the driver class" do
   pdf = Factory.create(:pdf)
   assert_equal GeneratePDF::Image, pdf.driver_class
   # Failure:
   # <GeneratePDF::Image> expected but was <Pdf::GeneratePDF::Image>.
 end
end

When I run the test with minitest it fails with following error:

Failure:
<GeneratePDF::Image> expected but was <Pdf::GeneratePDF::Image>.

But the same testcase is passed with UnitTest.

Work around

I am able to resolve the issue by prefixing the scope resolution in return statement.

class Pdf
 def generator_class
   return ::GeneratePDF::Image
 end
end

Please let me know why its behaving differently and best way to resolve this issue.

Aucun commentaire:

Enregistrer un commentaire