我用命名空间 Posts 编写了一个 Rails 3.1 引擎。因此,我的控制器位于 app/controllers/posts/ 中,我的模型位于 app/models/posts 等中。我可以很好地测试模型。一种型号的规格看起来像...
module Posts
describe Post do
describe 'Associations' do
it ...
end
...一切正常。
但是,控制器的规格不起作用。Rails 引擎安装在 /posts,但控制器是 Posts::PostController。因此,测试寻找控制器路由为帖子/帖子。
describe "GET index" do
it "assigns all posts as @posts" do
Posts::Post.stub(:all) { [mock_post] }
get :index
assigns(:posts).should eq([mock_post])
end
end
产生...
1) Posts::PostsController GET index assigns all posts as @posts
Failure/Error: get :index
ActionController::RoutingError:
No route matches {:controller=>"posts/posts"}
# ./spec/controllers/posts/posts_controller_spec.rb:16
我在测试应用程序的路由文件中尝试了各种技巧...:命名空间等,但无济于事。
我该如何进行这项工作?似乎不会,因为引擎将控制器放在/posts,但命名空间将控制器放在/posts/posts 以进行测试。