Added some unit and functional tests for the assets

Hesham E committed Sep 01, 2010
commit 3fa51674fc33b3227a7c418ad0c014024ac0f4c4
Showing 6 changed files with 41 additions and 17 deletions
app/controllers/cms_admin/assets_controller.rb +4 -1
@@ @@ -7,11 +7,14 @@ class CmsAdmin::AssetsController < CmsAdmin::BaseController
def create
@cms_asset = CmsAsset.create!(:uploaded_file => params[:file])
- render(:partial => 'cms_admin/assets/asset', :object => @cms_asset)
+ respond_to do |format|
+ format.js { render(:partial => 'cms_admin/assets/asset', :object => @cms_asset) }
+ end
end
def destroy
@cms_asset.destroy
+ format.js
end
protected
test/fixtures/cms_assets.yml +4 -11
@@ @@ -1,11 +1,4 @@
- # Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html
-
- # This model initially had no columns defined. If you add columns to the
- # model remove the '{}' from the fixture names and add the columns immediately
- # below each fixture, per the syntax in the comments below
- #
- one: {}
- # column: value
- #
- two: {}
- # column: value
+ default:
+ file_file_name: sample.jpg
+ file_content_type: image/jpeg
+ file_file_size: 20099
\ No newline at end of file
test/fixtures/files/valid_image.jpg +0 -0
test/functional/cms_admin/assets_controller_test.rb +19 -3
@@ @@ -1,8 +1,24 @@
require 'test_helper'
class CmsAdmin::AssetsControllerTest < ActionController::TestCase
- # Replace this with your real tests.
- test "the truth" do
- assert true
+
+ def test_index
+ get :index
+ assert_response :success
end
+
+ def test_create
+ assert_difference 'CmsAsset.count', 1 do
+ xhr :post, :create, :file => fixture_file_upload('files/valid_image.jpg')
+ assert_response :success
+ end
+ end
+
+ def test_create
+ assert_difference 'CmsAsset.count', -1 do
+ xhr :delete, :destroy, :id => cms_assets(:default)
+ assert_response :success
+ end
+ end
+
end
test/test_helper.rb +1 -0
@@ @@ -4,6 +4,7 @@ require 'rails/test_help'
class ActiveSupport::TestCase
fixtures :all
+ include ActionDispatch::TestProcess
# Example usage:
# assert_has_errors_on( @record, [:field_1, :field_2] )
test/unit/cms_asset_test.rb +13 -2
@@ @@ -2,8 +2,19 @@ require File.dirname(__FILE__) + '/../test_helper'
class CmsAssetTest < ActiveSupport::TestCase
- def test_something
- flunk
+ def test_validations
+ asset = CmsAsset.create
+ assert asset.errors.present?
+ assert_has_errors_on asset, [:file_file_name]
+ end
+
+ def test_create
+ assert_difference 'CmsAsset.count', 2 do
+ asset = CmsAsset.create(:file => fixture_file_upload('files/valid_image.jpg', 'image/jpeg'))
+ assert asset.image?
+ asset = CmsAsset.create(:uploaded_file => fixture_file_upload('files/valid_image.jpg'))
+ assert asset.image?
+ end
end
end