本文介绍了如何在rspec中编写Rails 3.1引擎控制器测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我用命名空间Posts编写了一个Rails 3.1引擎。因此,可以在app / controllers / posts /中找到我的控制器,在app / models / posts中找到我的模型,等等。我可以很好地测试模型。一种模型的规格看起来像... I have written a Rails 3.1 engine with the namespace Posts. Hence, my controllers are found in app/controllers/posts/, my models in app/models/posts, etc. I can test the models just fine. The spec for one model looks like...module Posts describe Post do describe 'Associations' do it ... end ...并且一切正常精细。 ... and everything works fine. 但是,控制器的规格不起作用。 Rails引擎安装在/ posts,但控制器为Posts :: PostController。因此,测试将控制器路由查找为发布/发布。 However, the specs for the controllers do not work. The Rails engine is mounted at /posts, yet the controller is Posts::PostController. Thus, the tests look for the controller route to be posts/posts. 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我尝试了测试应用程序的路由文件中的各种技巧...:namespace等,但都无济于事。 I've tried all sorts of tricks in the test app's routes file... :namespace, etc, to no avail. 我如何进行这项工作?似乎不会,因为引擎将控制器放在/ posts,但是命名空间将控制器放在/ posts / posts进行测试。 How do I make this work? It seems like it won't, since the engine puts the controller at /posts, yet the namespacing puts the controller at /posts/posts for the purpose of testing. 推荐答案我假设您正在使用虚拟Rails应用测试引擎,例如引擎。I'm assuming you're testing your engine with a dummy rails app, like the one that would be generated by enginex.您的引擎应安装在虚拟应用程序:Your engine should be mounted in the dummy app:在 spec / dummy / config / routes.rb :Dummy::Application.routes.draw do mount Posts::Engine => '/posts-prefix'end我的第二个假设是您的引擎是孤立的: My second assumption is that your engine is isolated:在 lib / posts.rb :module Posts class Engine < Rails::Engine isolate_namespace Posts endend I不知道这两个假设是否真的必要,但这就是我自己的引擎的结构。I don't know if these two assumptions are really required, but that is how my own engine is structured.解决方法很简单,而不是这个The workaround is quite simple, instead of thisget :show, :id => 1使用此get :show, {:id => 1, :use_route => :posts} :posts 符号应是引擎的名称,而不是引擎的安装路径。The :posts symbol should be the name of your engine and NOT the path where it is mounted.之所以可行,是因为get方法参数直接传递给 ActionDispatch ::路由:: RouteSet :: Generator#initialize (定义此处),依次使用 @named_route 从 Rack获取正确的路由:: Mount :: RouteSet#generate (请参见此处和This works because the get method parameters are passed straight to ActionDispatch::Routing::RouteSet::Generator#initialize (defined here), which in turn uses @named_route to get the correct route from Rack::Mount::RouteSet#generate (see here and here).插入rails内部很有趣,但是很费时间,我不会每天都这样做。 -)。Plunging into the rails internals is fun, but quite time consuming, I would not do this every day ;-) . HTH 这篇关于如何在rspec中编写Rails 3.1引擎控制器测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-21 03:34