"article/...">

Testing general controller action with rspec

Here's what my routes look like:

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

This is what my controller looks like:

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

I want to check the save_tags action, so I write my spec as follows:

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 specification, I get an error

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

I think the problem is that the save_tags action is a general controller action, i.e. there is no / article /: id / save_tags in the routes. What is the best way to test the action of this controller?

+4
source share
2 answers

. , , :id, . post :save_tags :id, , , , , article_id.

:

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

Rails , :action , , action , Rails . , :

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

:

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

.

post '/article/:id/save_tags' 

# 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
0

All Articles