运行初始测试后测试数据库未刷新
我正在浏览 Railstutorial.org 并遇到这个问题:
运行测试后测试数据库似乎没有清空。
在第 6.10 节中,我在 user_spec.rb 文件中创建了下面的行,它第一次通过,然后每次都失败。
it "should create a new instance given valid attributes" do
User.create!(@attr)
end
当我查看 test.sqlite3 数据库时,它显示数据库中有 1 个用户。如果我删除用户,测试会通过一次,然后再次失败,表示无法创建用户,因为电子邮件不是唯一的。我查看数据库,记录又出现了。
看起来上面的代码创建了它,但它永远不会被销毁。
这是我的 GemFile
source 'http://rubygems.org'
gem 'rails', '3.0.3'
gem 'sqlite3-ruby', '1.3.2', :require => 'sqlite3'
gem 'gravatar_image_tag', '0.1.0'
group :development do
gem 'rspec-rails', '2.3.0'
gem 'annotate-models', '1.0.4'
end
group :test do
gem 'rspec', '2.3.0'
gem 'webrat', '0.7.1'
gem 'spork', '0.8.4'
end
I am going thru Railstutorial.org and having this issue:
Test Database seem to not be emptying after running tests.
In section 6.10 I create the line below in the user_spec.rb file and it passes the first time then fails every time afterwards.
it "should create a new instance given valid attributes" do
User.create!(@attr)
end
When i look at the test.sqlite3 database it shows 1 user in the database. If I remove the user the test passes once, then fails again saying it cannot create the user because email is not unique. I look in the Database and there the record is again.
It seems like that code above creates it, but it doesnt ever get destroyed.
Here is my GemFile
source 'http://rubygems.org'
gem 'rails', '3.0.3'
gem 'sqlite3-ruby', '1.3.2', :require => 'sqlite3'
gem 'gravatar_image_tag', '0.1.0'
group :development do
gem 'rspec-rails', '2.3.0'
gem 'annotate-models', '1.0.4'
end
group :test do
gem 'rspec', '2.3.0'
gem 'webrat', '0.7.1'
gem 'spork', '0.8.4'
end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
检查
./spec/spec_helper.rb
并确保存在以下行:config.use_transactional_fixtures = true
Check in
./spec/spec_helper.rb
and make sure the following line is there:config.use_transactional_fixtures = true
查看 database_cleaner gem 并看看是否可以解决您的问题。
Have a look at the database_cleaner gem and see if that solves your problem.