即使在执行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
有什么想法吗?谢谢
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迁移运行迁移。
尝试重新创建测试数据库
无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