[英]Connecting to MySQL DB in Ruby Rspec
[n00b警报]我可能做错了一切……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,数据库I / O使用ActiveRecord,等一切安好WORKS除了RSpec的测试方法。 代码(忽略了不相关的部分):
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
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和server的最新版本。 在Windows 7 x86上。 Ruby 1.9.2。 谢谢。
我猜测当您运行rspec测试时,并非所有内容都已正确加载。 运行rspec时,是否所有正确设置了数据库连接的类并使用正确的参数加载?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.