【问题标题】:ruby on rails 4: could not find table 'table_name' even after a rake db:test:prepareruby on rails 4:即使在 rake db:test:prepare 之后也找不到表 'table_name'
【发布时间】:2013-08-02 07:20:18
【问题描述】:

我收到以下错误,即使在执行rake db:test:prepare 之后也是如此。我正在运行 rails 4.0。

1) Core::PostsController GET index assigns all posts as @posts
     Failure/Error: post = Post.create! valid_attributes
     ActiveRecord::StatementInvalid:
       Could not find table 'core_posts'
     # ./spec/controllers/core/posts_controller_spec.rb:36:in `block (3 levels) in <module:Core>'

我在引擎中运行这个测试,所以它可能是相关的吗?我的测试如下所示:

module Core
  describe PostsController do

    # This should return the minimal set of attributes required to create a valid
    # Post. As you add validations to Post, be sure to
    # adjust the attributes here as well.
    let(:valid_attributes) { {  } }

    # This should return the minimal set of values that should be in the session
    # in order to pass any filters (e.g. authentication) defined in
    # PostsController. Be sure to keep this updated too.
    let(:valid_session) { {} }

    describe "GET index" do
      it "assigns all posts as @posts" do
        post = Post.create! valid_attributes
        get :index, {}, valid_session
        assigns(:posts).should eq([post])
      end
    end


  end
end

有什么想法吗?谢谢!

【问题讨论】:

  • 你真的创建了表吗?
  • 我认为 rake db:test:prepare 创建了它...
  • 仅当它存在于您的开发数据库和架构文件中时。 Rails 不是通灵的。
  • 是的,所以我先做了 rails db:migrate,然后是 rails db:test:prepare。架构确实有:create_table "core_posts", force: true do |t| t.datetime "created_at" t.datetime "updated_at" 结束
  • 我刚刚意识到我的主应用程序中的 development.sqlite3 有数据,但是我的引擎中的 development.sqlite3 是空的。我正在从我的引擎内部运行 rspec。我猜是因为这个?

标签: ruby-on-rails rspec ruby-on-rails-4


【解决方案1】:

cd 进入引擎目录并生成一个虚拟应用程序以测试您的引擎:

rails plugin new . --full --mountable --dummy-path spec/dummy

上面的命令将生成一个具有隔离命名空间的完全可挂载引擎,这意味着来自该引擎的所有控制器和模型都将被隔离在引擎的命名空间内。例如,Post 模型稍后将被称为Core::Post,而不仅仅是Post。由于您已经生成了应用程序——如果发生冲突,您可以跳过更改。

此外,引擎附带一个虚拟应用程序,位于spec/dummy,因为我们通过--dummy_path 选项告诉它这样做。这个虚拟应用程序只是一个简单的 Rails 应用程序,可用于测试引擎,就像它安装在真实应用程序中一样。

然后,您需要更改 rspec 以使用此虚拟应用程序,方法是进行以下更改:

spec/spec_helper.rb里面改这一行

require File.expand_path("../../config/environment", __FILE__)

到这里

require File.expand_path("../dummy/config/environment",__FILE__)

因为config/environment.rb 文件不存在两个目录,而是在spec/dummy 内部。

现在,您可以通过以下命令运行迁移。

RAILS_ENV=test bin/rake db:migrate

为什么不使用 db:test:prepare?

我们无法运行 rake db:test:prepare,因为它不可用。这 db:migrate 任务特别针对引擎进行了更改,并将在 spec/dummy/db 文件夹中运行引擎 PLUS 迁移的迁移。

【讨论】:

  • env RAILS_ENV=test rake db:migrate
  • 迁移后,我可以在哪里运行测试(bundle exec rspec spec)?在引擎根目录中还是在虚拟目录中?谢谢
【解决方案2】:

尝试重新创建您的测试数据库

没有 rbenv

RAILS_ENV=test rake db:drop
RAILS_ENV=test rake db:create
RAILS_ENV=test rake db:test:prepare

使用 rbenv

RAILS_ENV=test bundle exec rake db:drop
RAILS_ENV=test bundle exec rake db:create
RAILS_ENV=test bundle exec rake db:test:prepare

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-04
    • 2012-10-13
    • 2015-08-05
    • 1970-01-01
    • 2012-10-20
    • 2013-07-30
    相关资源
    最近更新 更多