Skip to content

Commit

Permalink
Fix (and cleanup) specs about tags controller
Browse files Browse the repository at this point in the history
  • Loading branch information
Yannick Francois committed Aug 26, 2013
1 parent 9b27b06 commit a3d7964
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 91 deletions.
2 changes: 1 addition & 1 deletion app/controllers/admin/tags_controller.rb
Expand Up @@ -2,7 +2,7 @@ class Admin::TagsController < Admin::BaseController
cache_sweeper :blog_sweeper

def index; redirect_to action: 'new' ; end
def new; new_or_edit; end
def new; new_or_edit; end
def edit; new_or_edit; end

def destroy
Expand Down
125 changes: 35 additions & 90 deletions spec/controllers/admin/tags_controller_spec.rb
@@ -1,110 +1,55 @@
require 'spec_helper'

describe Admin::TagsController do
before do
create :blog
henri = create :user, login: 'henri', profile: create(:profile_admin)
request.session = { :user => henri.id }
end

describe 'index action' do
before :each do
get :index
end
let!(:blog) { create(:blog) }
let!(:user) { create(:user, login: 'henri', profile: create(:profile_admin)) }

it 'should be success' do
response.should be_success
end
before { request.session = { user: user.id } }

it 'should render template index' do
response.should render_template('index')
end
describe :index do
before(:each) { get :index }
it { expect(response).to redirect_to(action: 'new') }
end

describe 'edit action' do
before(:each) do
tag_id = FactoryGirl.create(:tag).id
get :edit, :id => tag_id
end

it 'should be success' do
response.should be_success
end

it 'should render template edit' do
response.should render_template('edit')
end
context "with a tag" do
let(:tag) { create(:tag) }

it 'should assigns value :tag' do
assert assigns(:tag).valid?
end
end
describe :edit do
before(:each) { get :edit, id: tag.id }

describe 'destroy action with GET' do
before(:each) do
@tag_id = create(:tag).id
get :destroy, :id => @tag_id
it { expect(response).to be_success }
it { expect(response).to render_template('new') }
it { expect(assigns(:tag)).to be_valid }
end

it 'should be success' do
response.should be_success
end
describe :destroy do
context "with a get" do
before(:each) { get :destroy, id: tag.id }

context "with view" do
render_views
it { expect(response).to be_success }
it { expect(response).to render_template('destroy') }
it { expect(assigns(:record)).to be_valid }

it 'should have an id in the form destination' do
response.should have_selector("form[action='/admin/tags/destroy/#{@tag_id}'][method='post']")
context "with view" do
render_views
it { expect(response).to have_selector("form[action='/admin/tags/destroy/#{tag.id}'][method='post']") }
end
end
end

it 'should render template edit' do
response.should render_template('destroy')
end

it 'should assigns value :tag' do
assert assigns(:record).valid?
end
end

describe 'destroy action with POST' do
before do
@tag = FactoryGirl.create(:tag)
post :destroy, 'id' => @tag.id, 'tag' => {:display_name => 'Foo Bar'}
end

it 'should redirect to index' do
response.should redirect_to(:action => 'index')
end

it 'should have one less tags' do
Tag.count.should == 0
end
end

describe 'update action' do
before do
@tag = FactoryGirl.create(:tag)
post :edit, 'id' => @tag.id, 'tag' => {:display_name => 'Foo Bar'}
end

it 'should redirect to index' do
response.should redirect_to(:action => 'index')
end

it 'should update tag' do
@tag.reload
@tag.name.should == 'foo-bar'
@tag.display_name.should == "Foo Bar"
context "with a post" do
before(:each) { post :destroy, id: tag.id, tag: {display_name: 'Foo Bar'}}
it { expect(response).to redirect_to(action: 'index') }
it { expect(Tag.count).to eq(0) }
end
end

it 'should create a redirect from the old to the new' do
old_name = @tag.name
@tag.reload
new_name = @tag.name

r = Redirect.find_by_from_path "/tag/#{old_name}"
r.to_path.should == "/tag/#{new_name}"
describe :update do
before(:each) { post :edit, id: tag.id, tag: {display_name: 'Foo Bar'} }
it { expect(response).to be_success }
it { expect(tag.reload.name).to eq('foo-bar') }
it { expect(tag.reload.display_name).to eq('Foo Bar') }
it { expect(Redirect.count).to eq(1) }
it { expect(Redirect.first.to_path).to eq('/tag/foo-bar') }
end
end
end

0 comments on commit a3d7964

Please sign in to comment.