【发布时间】:2010-12-09 16:54:27
【问题描述】:
[n00b alert]我可能做错了... RSpec 输出此失败:
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 <top (required)>'
我在同一台机器上设置了一个 MySQL 数据库。该程序提供了一种用于计算图的最小生成树的算法。具有用于文件 I/O、使用 ActiveRecord 的数据库 I/O 等的方法。除了 RSpec 测试之外,所有 WORKS WELL。 代码(无关部分省略):
prim_spec.rb
describe PGraph, "online" do
before (:all) do
ActiveRecord::Base.establish_connection(
:adapter => "mysql2",
:host => "localhost",
:username => "root",
:password => "xxxxx",
:database => "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
prim.rb
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
连接和 PGraph 操作在 ui.rb 中执行。 那么,嗯,访问真实数据库(我很懒)进行测试的正确方法是什么(或者是其他地方的问题?)?最好是一些简单的东西,因为这只是一个学校作业。并且不会弄乱 Rails 或其他宝石。 PS:我使用的是所有 gems 和服务器的最新版本。在 Windows 7 x86 上。红宝石 1.9.2。谢谢。
【问题讨论】:
标签: mysql ruby activerecord rspec