mko
mko

Reputation: 22114

Can't create instance variable using before_suite method in miniTest

I want create a @conn before all test case and can be used for all test case so i did something like:

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

And I got an error:

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

it seems like the @conn can't be accessed in the test case,

Upvotes: 2

Views: 113

Answers (1)

blowmage
blowmage

Reputation: 8984

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

Upvotes: 1

Related Questions