2

我想在所有测试用例之前创建一个@conn并且可以用于所有测试用例,所以我做了类似的事情:

class SQLTest < Test::Unit::TestCase
    def self.before_suite                                                       
        @conn = PG.connect(dbname:'MapSwitcher',host:'localhost', user:'Lin')
    end
    def  test_employee_table_have_5_entries
        result = @conn.exec("SELECT COUNT(*) FROM employees")
        assert_equal(result.getvalue(0,0).to_i, 5)
     end

end

我得到一个错误:

noMethodError: private method `exec' called for nil:NilClass

似乎@conn在测试用例中无法访问,

4

1 回答 1

1
class SQLTest < MiniTest::Unit::TestCase
  def setup
    @conn = PG.connect(dbname:'MapSwitcher',host:'localhost', user:'Lin')
  end

  def test_employee_table_have_5_entries
    result = @conn.exec("SELECT COUNT(*) FROM employees")
    assert_equal(result.getvalue(0,0).to_i, 5)
  end
end
于 2013-09-03T13:51:59.750 回答