title: Connecting to MySQL DB in Ruby Rspec
body: <p><strong>[n00b alert]</strong> I'm probably doing this all wrong... RSpec outputs this failure:</p>
  
  <pre><code>1)...                                       #skipped irrelevant info
     Failure/Error: graph.read_db('example1')
     Not connected to any DB.                 #error msg
     #./prim.rb:135:in 'read_db'
     #./prim_spec.rb:171:in 'block (2 levels) in &lt;top (required)&gt;'
  </code></pre>
  
  <p>I have set up a MySQL database on the same machine. The program provides an algorithm for computing a graph's minimum spanning tree. Has methods for file I/O, database I/O using ActiveRecord, etc. All <strong>WORKS WELL</strong> except RSpec tests.
  Code (irrelevant parts left out):</p>
  
  <h1>prim_spec.rb</h1>
  
  <pre><code>describe PGraph, "online" do
      before (:all) do
          ActiveRecord::Base.establish_connection(
          :adapter =&gt; "mysql2",
          :host =&gt; "localhost",
          :username =&gt; "root",
          :password =&gt; "xxxxx",
          :database =&gt; "rubytasks" )
          #the exact same statement works perfectly when running the program itself, but fails in RSpec
      end
      before (:each) do
          @graph = PGraph.new
      end
  
      it "should correctly retrieve data from database" do
          @graph.read_db('example1')   #line 171
          #business part goes here
      end
  end
  </code></pre>
  
  <h1>prim.rb</h1>
  
  <pre><code>class PGraph
      def read_db(graphID)
          #the error which is raised (line 135):
          raise "Not connected to any DB." unless ActiveRecord::Base.connected?
          #reading goes here
      end
  end
  </code></pre>
  
  <p>Connection and PGraph manipulation is performed in ui.rb.
  So, ummm, what's the correct way to access a real DB (I'm lazy) for testing (or is the problem elsewhere?)? Preferably something simple, since this is just a school assignment. And without messing with Rails or other gems.
  PS: I'm using the most recent versions of all gems and server. On Windows 7 x86. Ruby 1.9.2. Thanks.</p>