require_relative "../test_helper" require 'follow_categories_controller' class FollowCategoriesControllerTest < ActionController::TestCase def setup @controller = FollowCategoriesController.new @person = create_user('person').person login_as(@person.identifier) end should 'return all categories of a profile' do category1 = Circle.create(:name => "category1", :person => @person) category2 = Circle.create(:name => "category2", :person => @person) get :index, :profile => @person.identifier assert_equivalent [category1, category2], assigns[:categories] end should 'initialize an empty category for creation' do get :new, :profile => @person.identifier assert_nil assigns[:follow_category].id assert_nil assigns[:follow_category].name end should 'create a new category' do assert_difference '@person.follow_categories.count' do post :create, :profile => @person.identifier, :follow_category => { :name => 'category' } end assert_redirected_to :action => :index end should 'not create a category without a name' do assert_no_difference '@person.follow_categories.count' do post :create, :profile => @person.identifier, :follow_category => { :name => nil } end assert_template :new end should 'retrieve an existing category when editing' do category = Circle.create(:name => "category", :person => @person) get :edit, :profile => @person.identifier, :id => category.id assert_equal category.name, assigns[:follow_category].name end should 'return 404 when editing a category that does not exist' do get :edit, :profile => @person.identifier, :id => "nope" assert_response 404 end should 'update an existing category' do category = Circle.create(:name => "category", :person => @person) get :update, :profile => @person.identifier, :id => category.id, :follow_category => { :name => "new name" } category.reload assert_equal "new name", category.name assert_redirected_to :action => :index end should 'not update an existing category without a name' do category = Circle.create(:name => "category", :person => @person) get :update, :profile => @person.identifier, :id => category.id, :follow_category => { :name => nil } category.reload assert_equal "category", category.name assert_template :edit end should 'return 404 when updating a category that does not exist' do get :update, :profile => @person.identifier, :id => "nope", :name => "new name" assert_response 404 end should 'destroy an existing category and update related profiles' do category = Circle.create(:name => "category", :person => @person) follower = fast_create(ProfileFollower, :profile_id => fast_create(Person).id, :follower_id => @person.id, :follow_category_id => category.id) assert_difference "@person.follow_categories.count", -1 do get :destroy, :profile => @person.identifier, :id => category.id end follower.reload assert_nil follower.follow_category end should 'return 404 when deleting and category that does not exist' do get :destroy, :profile => @person.identifier, :id => "nope" assert_response 404 end should 'display notice when ' do category = Circle.create(:name => "category", :person => @person) Circle.any_instance.stubs(:destroy).returns(false) get :destroy, :profile => @person.identifier, :id => category.id assert_not_nil session[:notice] end end