我希望在所有测试用例之前创建一个@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
,
答案 0 :(得分: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