RSpec 失败:迁移后找不到表...?
我有一个带有一个模型的裸 Rails 3 应用程序,使用 rails g model User
生成。
我添加了一个工厂(使用 factory_girl_rails
):
Factory.define :user do |f|
f.email "[email protected]"
f.password "blah"
f.password_confirmation "blah"
f.display_name "neezer"
end
然后我添加了一个测试:
require 'spec_helper'
describe User do
subject { Factory :user }
it "can be created from a factory" do
subject.should_not be_nil
subject.should be_kind_of User
end
end
然后我使用 rake db:migrate
迁移我的数据库 。
然后我使用 rspec spec 运行测试,测试失败并显示以下内容:
Failures:
1) User can be created from a factory
Failure/Error: subject { Factory :user }
ActiveRecord::StatementInvalid:
Could not find table 'users'
# ./spec/models/user_spec.rb:5:in `block (2 levels) in <top (required)>'
# ./spec/models/user_spec.rb:8:in `block (2 levels) in <top (required)>'
我很困惑,因为我刚刚迁移了数据库和我的 schema.db 文件反映存在一个 users 表,那么给出了什么?
我知道这是一个初学者的问题,但是把我的头撞在墙上是行不通的......
factory_girl (1.3.3)
factory_girl_rails (1.0.1)
rails (3.0.5)
rspec-rails (2.5.0)
sqlite3 (1.3.3)
I have a naked rails 3 app with one model, generated using rails g model User
.
I've added a factory (using factory_girl_rails
):
Factory.define :user do |f|
f.email "[email protected]"
f.password "blah"
f.password_confirmation "blah"
f.display_name "neezer"
end
Then I've added one test:
require 'spec_helper'
describe User do
subject { Factory :user }
it "can be created from a factory" do
subject.should_not be_nil
subject.should be_kind_of User
end
end
Then I migrate my database using rake db:migrate
.
Then I run the test using rspec spec
, and the test fails with the following:
Failures:
1) User can be created from a factory
Failure/Error: subject { Factory :user }
ActiveRecord::StatementInvalid:
Could not find table 'users'
# ./spec/models/user_spec.rb:5:in `block (2 levels) in <top (required)>'
# ./spec/models/user_spec.rb:8:in `block (2 levels) in <top (required)>'
I'm confused, because I did just migrate my database, and my schema.db
file reflects that there is a users table present, so what gives?
I know this is a beginner question, but banging my head against a wall isn't working...
factory_girl (1.3.3)
factory_girl_rails (1.0.1)
rails (3.0.5)
rspec-rails (2.5.0)
sqlite3 (1.3.3)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试执行
这应该可以修复您的测试数据库。
Try to execute
This should fix your tests db.
这里的要点是 rspec 命令不会在测试数据库上执行迁移。并且
rake db:migrate
仅在您当前的环境(可能是开发
)中运行迁移。其他环境(例如生产
和测试
)在没有这些更改的情况下结束。您可以运行
这将准备您的测试数据库(使用
schema.rb
删除和创建)并运行所有测试。正如另一个答案所建议的,这:
还将设置您的测试数据库,但之后您必须运行 rspec 命令,所以,我个人更喜欢第一个选择。
The point here is that
rspec
command doesn't execute migrations on your test database. andrake db:migrate
only runs migrations in your current environment, probablydevelopment
. Others environment likeproduction
andtest
ends without having those changes.You can run
That will prepare your testing db (drop and create using
schema.rb
) and run all tests.As the other answer suggested, this:
Will also setup your testing db, but you have to run the rspec command after that, so, personally I prefer the first option.
试试这个:
尝试使用
try this out:
try using