RSpec 失败:迁移后找不到表...?

发布于 2024-10-21 18:23:19 字数 1414 浏览 3 评论 0原文

我有一个带有一个模型的裸 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

因为看清所以看轻 2024-10-28 18:23:19

尝试执行

rake db:test:prepare

这应该可以修复您的测试数据库。

Try to execute

rake db:test:prepare

This should fix your tests db.

永言不败 2024-10-28 18:23:19

这里的要点是 rspec 命令不会在测试数据库上执行迁移。并且 rake db:migrate 仅在您当前的环境(可能是开发)中运行迁移。其他环境(例如生产测试)在没有这些更改的情况下结束。

您可以运行

rake spec

这将准备您的测试数据库(使用schema.rb删除和创建)并运行所有测试。

正如另一个答案所建议的,这:

rake db:test:prepare

还将设置您的测试数据库,但之后您必须运行 rspec 命令,所以,我个人更喜欢第一个选择。

The point here is that rspec command doesn't execute migrations on your test database. and rake db:migrate only runs migrations in your current environment, probably development. Others environment like production and test ends without having those changes.

You can run

rake spec

That will prepare your testing db (drop and create using schema.rb) and run all tests.

As the other answer suggested, this:

rake db:test:prepare

Will also setup your testing db, but you have to run the rspec command after that, so, personally I prefer the first option.

无尽的现实 2024-10-28 18:23:19

试试这个:

For rails version > 4.1+ this solution will work as the current scenario.

but in Rails 4.1+, rake db:test:prepare is deprecated.

尝试使用

rake db:migrate RAILS_ENV=test (it will work for all version of rails)

try this out:

For rails version > 4.1+ this solution will work as the current scenario.

but in Rails 4.1+, rake db:test:prepare is deprecated.

try using

rake db:migrate RAILS_ENV=test (it will work for all version of rails)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文