简体   繁体   English

使用rspec测试常规控制器操作

[英]Testing a general controller action using rspec

Here's how my routes look like: 这是我的路线的样子:

 /article/:id/:action     {:root=>"article", :controller=>"article/article", :title=>"Article"}

Here's how my controller looks like: 这是我的控制器的样子:

# app/controllers/article/article_controller.rb
class ArticleController < ApplicationController
  def save_tags
    # code here
  end
end

I want to test the save_tags action so I write my spec like this: 我想测试save_tags动作,所以我写这样的规范:

describe ArticleController do       
   context 'saving tags' do
     post :save_tags, tag_id => 123, article_id => 1234
     # tests here
   end
end

But when I run this spec, I get the error 但是当我运行这个规范时,我得到了错误

ActionController::RoutingError ...
No route matches {:controller=>"article/article", :action=>"save_tags"}

I think the issue is the save_tags action is a general controller action, ie. 我认为问题是save_tags动作是一般控制器动作,即。 there's no /article/:id/save_tags in routes. 路线中没有/ article /:id / save_tags。 What's the best way to test this controller action? 测试此控制器操作的最佳方法是什么?

You're spot on. 你是现货。 The issue is that you're looking for a route which doesn't have :id in it, but you don't have one. 问题是你正在寻找一条没有:id的路线,但你没有。 You'll need to pass a parameter to the post :save_tags of :id , and given the above question, I believe it is what you are calling article_id . 你需要将一个参数传递给post :save_tags :id ,并且给出上述问题,我相信你正在调用article_id

Therefore, try changing your test to: 因此,请尝试将测试更改为:

describe ArticleController do       
   context 'saving tags' do
     post :save_tags, tag_id => 123, id => 1234
     # tests here
   end
end

Update 更新

Rails might be getting confused because you're using :action in your route and I believe action is either a reserved word or a word that Rails treats as special. Rails可能会因为你在你的路线中使用:action而感到困惑,我相信action要么是保留的单词,要么是Rails认为特殊的单词。 Maybe try changing your routes to: 也许尝试将您的路线更改为:

/article/:id/:method_name {:root=>"article", :controller=>"article/article", :title=>"Article"}

And your test to: 你的测试:

describe ArticleController do       
  context 'saving tags' do
    post :save_tags, { :tag_id => 123, :article_id => 1234, :method_name => "save_tags" }
    # tests here
  end
end

You need a route to map to your controller actions 您需要一个映射到控制器操作的路径

post '/article/:id/save_tags' 

should work, or consider using resources helper to build your routes 应该工作,或考虑使用资源助手来建立你的路线

# creates the routes new, create, edit, update, show, destroy, index
resources :articles

# you can exclude any you do not want
resources :articles, except: [:destroy]

# add additional routes that require an article in the member block
resources :articles do 
  member do 
    post 'save_tags'
  end
end

# add additional routes that do NOT require an article in the collection block
resources :articles do 
  collection do 
    post 'publish_all'
  end
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM