From f84a978e0180e6f7c33d0490823119300c01d2a9 Mon Sep 17 00:00:00 2001 From: Yannick Francois Date: Mon, 5 Aug 2013 16:39:47 +0200 Subject: [PATCH] Clean specs and remove little duplication --- app/controllers/admin/base_controller.rb | 9 ++ .../admin/categories_controller.rb | 15 +-- .../admin/post_types_controller.rb | 21 +--- app/controllers/admin/redirects_controller.rb | 13 +-- .../admin/categories_controller_spec.rb | 106 +++++++----------- .../admin/post_types_controller_spec.rb | 92 +++++++-------- 6 files changed, 101 insertions(+), 155 deletions(-) diff --git a/app/controllers/admin/base_controller.rb b/app/controllers/admin/base_controller.rb index ffe0252cb7..f26b207a7c 100644 --- a/app/controllers/admin/base_controller.rb +++ b/app/controllers/admin/base_controller.rb @@ -17,6 +17,15 @@ def insert_editor private + def save_a(object, title) + if object.save + flash[:notice] = _("#{title.capitalize} was successfully saved.") + else + flash[:error] = _("#{title.capitalize} could not be saved.") + end + redirect_to action: 'index' + end + def destroy_a(klass_to_destroy) @record = klass_to_destroy.find(params[:id]) return render('admin/shared/destroy') unless request.post? diff --git a/app/controllers/admin/categories_controller.rb b/app/controllers/admin/categories_controller.rb index caf46bbd90..7b310e0965 100644 --- a/app/controllers/admin/categories_controller.rb +++ b/app/controllers/admin/categories_controller.rb @@ -1,7 +1,7 @@ class Admin::CategoriesController < Admin::BaseController cache_sweeper :blog_sweeper - def index; redirect_to :action => 'new' ; end + def index; redirect_to action: 'new' ; end def edit; new_or_edit; end def new @@ -30,7 +30,7 @@ def new_or_edit @category.attributes = params[:category] if request.post? respond_to do |format| - format.html { save_category } + format.html { save_a(@category, 'category') } format.js do @category.save @article = Article.new @@ -39,17 +39,8 @@ def new_or_edit end end return - end - render 'new' - end - - def save_category - if @category.save! - flash[:notice] = _('Category was successfully saved.') else - flash[:error] = _('Category could not be saved.') + render 'new' end - redirect_to :action => 'new' end - end diff --git a/app/controllers/admin/post_types_controller.rb b/app/controllers/admin/post_types_controller.rb index 8b802a08d5..ec69de4503 100644 --- a/app/controllers/admin/post_types_controller.rb +++ b/app/controllers/admin/post_types_controller.rb @@ -21,27 +21,14 @@ def destroy def new_or_edit @post_types = PostType.find(:all) - @post_type = case params[:id] - when nil - PostType.new - else - PostType.find(params[:id]) - end + @post_type = PostType.where(id: params[:id]).first + @post_type ||= PostType.new @post_type.attributes = params[:post_type] if request.post? - save_post_type - return - end - render 'new' - end - - def save_post_type - if @post_type.save! - flash[:notice] = _('Post Type was successfully saved.') + save_a(@post_type, 'Post Type') else - flash[:error] = _('Post Type could not be saved.') + render 'new' end - redirect_to :action => 'index' end end diff --git a/app/controllers/admin/redirects_controller.rb b/app/controllers/admin/redirects_controller.rb index a9983b7910..6afd460a11 100644 --- a/app/controllers/admin/redirects_controller.rb +++ b/app/controllers/admin/redirects_controller.rb @@ -2,12 +2,12 @@ class Admin::RedirectsController < Admin::BaseController def index; redirect_to :action => 'new' ; end def edit; new_or_edit; end def new; new_or_edit; end - + def destroy @record = Redirect.find(params[:id]) return(render 'admin/shared/destroy') unless request.post? - + @record.destroy flash[:notice] = _('Redirection was successfully deleted.') redirect_to :action => 'index' @@ -16,7 +16,7 @@ def destroy private def new_or_edit @redirects = Redirect.where("origin is null").order('created_at desc').page(params[:page]).per(this_blog.admin_display_elements) - + @redirect = case params[:id] when nil Redirect.new @@ -36,11 +36,6 @@ def new_or_edit end def save_redirect - if @redirect.save! - flash[:notice] = _('Redirection was successfully saved.') - else - flash[:error] = _('Redirection could not be saved.') - end - redirect_to :action => 'index' + save_a(@redirect, 'redirection') end end diff --git a/spec/controllers/admin/categories_controller_spec.rb b/spec/controllers/admin/categories_controller_spec.rb index c5d7c20477..5f7785016a 100644 --- a/spec/controllers/admin/categories_controller_spec.rb +++ b/spec/controllers/admin/categories_controller_spec.rb @@ -4,85 +4,65 @@ render_views before(:each) do - FactoryGirl.create(:blog) - #TODO Delete after removing fixtures - Profile.delete_all - henri = FactoryGirl.create(:user, :login => 'henri', :profile => FactoryGirl.create(:profile_admin, :label => Profile::ADMIN)) - request.session = { :user => henri.id } + create(:blog) + henri = create(:user, login: 'henri', profile: create(:profile_admin, label: Profile::ADMIN)) + request.session = { user: henri.id } end - it "test_index" do - get :index - assert_response :redirect, :action => 'index' + describe :index do + before(:each) { get :index } + it { expect(response).to redirect_to(action: 'new') } end - it "test_create" do - cat = FactoryGirl.create(:category) - Category.should_receive(:find).with(:all).and_return([]) - Category.should_receive(:new).and_return(cat) - cat.should_receive(:save!).and_return(true) - post :edit, 'category' => { :name => "test category" } - assert_response :redirect - assert_redirected_to :action => 'new' - end - - describe "test_new" do - before(:each) do - get :new + describe :edit do + context "when no category exist" do + before(:each) { post :edit, category: { name: "test category" }} + it { expect(response).to redirect_to(action: 'index') } end - it 'should render template view' do - assert_template 'new' - assert_tag :tag => "table", - :attributes => { :id => "category_container" } - end - end + context "with an existing category" do + let!(:category) { create(:category) } - describe "test_edit" do - before(:each) do - get :edit, :id => FactoryGirl.create(:category).id - end + context "when use post method" do + before(:each) { post :edit, id: category.id, category: { name: "new category name" }} + it { expect(response).to redirect_to(action: 'index') } + end - it 'should render template new' do - assert_template 'new' - assert_tag :tag => "table", - :attributes => { :id => "category_container" } - end + context "when use get method" do + before(:each) { get :edit, id: create(:category).id } - it 'should have valid category' do - assigns(:category).should_not be_nil - assert assigns(:category).valid? - assigns(:categories).should_not be_nil + context "with an existing category" do + let!(:category) { create(:category) } + + it { expect(response).to render_template('new') } + it { expect(response.body).to have_selector('table', id: 'category_container')} + it { expect(assigns(:category)).to_not be_nil } + it { expect(assigns(:category)).to be_valid } + it { expect(assigns(:categories)).to_not be_nil } + end + end end end - it "test_update" do - post :edit, :id => FactoryGirl.create(:category).id - assert_response :redirect, :action => 'index' + describe :new do + before(:each) { get :new } + it { expect(response).to render_template('new') } + it { expect(response.body).to have_selector('table', id: 'category_container')} end - describe "test_destroy with GET" do - before(:each) do - test_id = FactoryGirl.create(:category).id - assert_not_nil Category.find(test_id) - get :destroy, :id => test_id - end + describe :destroy do + let!(:category) { create(:category) } - it 'should render destroy template' do - assert_response :success - assert_template 'destroy' + context "with get method" do + before(:each) { get :destroy, id: category.id } + it { expect(response).to be_success } + it { expect(response).to render_template('destroy') } end - end - it "test_destroy with POST" do - test_id = FactoryGirl.create(:category).id - assert_not_nil Category.find(test_id) - get :destroy, :id => test_id - - post :destroy, :id => test_id - assert_response :redirect, :action => 'index' - - assert_raise(ActiveRecord::RecordNotFound) { Category.find(test_id) } + context "with post method" do + before(:each) { post :destroy, id: category.id } + it { expect(response).to redirect_to(action: 'index') } + it { expect(Category.count).to eq(0) } + end end - end diff --git a/spec/controllers/admin/post_types_controller_spec.rb b/spec/controllers/admin/post_types_controller_spec.rb index 746eecd38b..ab19b9362b 100644 --- a/spec/controllers/admin/post_types_controller_spec.rb +++ b/spec/controllers/admin/post_types_controller_spec.rb @@ -2,75 +2,59 @@ describe Admin::PostTypesController do render_views - before do - FactoryGirl.create(:blog) - #TODO delete this after remove fixture - Profile.delete_all - @user = FactoryGirl.create(:user, :profile => FactoryGirl.create(:profile_admin, :label => Profile::ADMIN)) - request.session = { :user => @user.id } - end - it "index shoudld redirect to new" do - get :index - assert_response :redirect, :action => 'new' + before do + create(:blog) + user = FactoryGirl.create(:user, profile: create(:profile_admin, :label => Profile::ADMIN)) + request.session = { user: user.id } end - it "test_create" do - pt = FactoryGirl.create(:post_type) - PostType.should_receive(:find).with(:all).and_return([]) - PostType.should_receive(:new).and_return(pt) - pt.should_receive(:save!).and_return(true) - post :edit, 'post_type' => { :name => "new post type" } - assert_response :redirect - assert_redirected_to :action => 'index' + describe :index do + before(:each) { get :index } + it { expect(response).to redirect_to(action: 'new') } end - describe "test_new" do - before(:each) do - get :new + describe :edit do + context "when create a new one" do + before(:each) {post :edit, post_type: {name: "new post type"}} + it { expect(response).to redirect_to(action: 'index') } + it { expect(PostType.count).to eq(1) } + it { expect(PostType.first.name).to eq("new post type") } end - it 'should render template new' do - assert_template 'new' + context "when update an existing one" do + let(:post_type) { create(:post_type, name: 'a name') } + before(:each) {post :edit, id: post_type.id, post_type: {name: 'an other name'}} + it { expect(response).to redirect_to(action: 'index') } + it { expect(PostType.count).to eq(1) } + it { expect(PostType.first.name).to eq('an other name') } end - end - - describe "test_edit" do - it 'should render template new' do - get :edit, :id => FactoryGirl.build(:post_type).id - assert_template 'new' - end - - it "test_update" do - post :edit, :id => FactoryGirl.create(:post_type).id - assert_response :redirect, :action => 'index' + context "when edit with a get method" do + before(:each) {get :edit, post_type: {name: "new post type"}} + it { expect(response).to render_template('new') } end + end - - describe "test_destroy with GET" do - before(:each) do - test_id = FactoryGirl.create(:post_type).id - assert_not_nil PostType.find(test_id) - get :destroy, :id => test_id - end - it 'should render destroy template' do - assert_response :success - assert_template 'destroy' - end + describe :new do + before(:each) { get :new } + it { expect(response).to render_template('new')} end - it "test_destroy with POST" do - test_id = FactoryGirl.create(:post_type).id - assert_not_nil PostType.find(test_id) - get :destroy, :id => test_id + describe :destroy do + let!(:post_type) { create(:post_type) } - post :destroy, :id => test_id - assert_response :redirect, :action => 'index' + context "with a get method" do + before(:each) { get :destroy, id: post_type.id } + it { expect(response).to be_success } + it { expect(response).to render_template('destroy') } + end - assert_raise(ActiveRecord::RecordNotFound) { PostType.find(test_id) } + context "with a post method" do + before(:each) { post :destroy, id: post_type.id } + it { expect(response).to redirect_to(action: 'index') } + it { expect(PostType.count).to eq(0) } + end end - - end