在Mini_Test和未初始化的常量错误中调用任何Ruby类

时间:2013-04-03 11:34:09

标签: ruby-on-rails ruby minitest

我想使用Mini_Test和Ruby进行DCI实现,但我无法在Mini_Test中调用AcademicYear类。我在下面的分隔文件中有一个简单的类和Mini_Test类;

/app/Models/academicyear.rb

class AcademicYear
  attr_accessor :year
end

/app/Test/Contexts/headteacher_define_academicyear_context_test.rb

require "minitest_helper"
class HeadTeacherDefineAcademicYearContextTest < MiniTest::Unit::TestCase
  def test_should_add_academic_year
    academicyear = AcademicYear.new
    academicyear.year= "2012-2013"
  end
end

当我创建一个新的AcademicYear对象时,我收到了这个错误;

HeadTeacherDefineAcademicYearContextTest#test_should_add_academic_year:
NameError: uninitialized constant HeadTeacherDefineAcademicYearContextTest::AcademicYear
    /home/developer/Desktop/dciexample/test/contexts/headteacher_define_academicyear_context_test.rb:6:in `test_should_add_academic_year'

我缺少什么,有什么想法吗?

1 个答案:

答案 0 :(得分:4)

你需要你的班级

require_relative '../../Models/academicyear.rb'

结果你有

/app/Test/Contexts/headteacher_define_academicyear_context_test.rb

require "minitest_helper"
require_relative '../../Models/academicyear.rb'

class HeadTeacherDefineAcademicYearContextTest < MiniTest::Unit::TestCase
  def test_should_add_academic_year
    academicyear = AcademicYear.new
    academicyear.year= "2012-2013"
  end
end