DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Functional Test Helper To Automate Basic CRUD Functions

03.09.2006
| 2357 views |
  • submit to reddit
        This is a funcitonal test helper, which can be used to create methods on the fly to iterate through your controller methods and test for validations, url parameters and expected responses. It keeps the tests from getting as cluttered. 
#expects a format like this:
	#test_required_attributes_in_controller User,
  	#					{:params=>{:id=>1,:user=>{}},:session=>{:user=>{}}},
	#					[:create, :update],
	#					:first_name, :last_name, :username, :email_address, :password

    def test_required_attributes_in_controller( model, options, actions,*required_fields )
    	
		sym = model.to_s.downcase.to_sym
		for field in required_fields
      		options[:params][sym][field] = ''
    	end
		
      	actions.each do |action|
        	self.class_eval do
          		define_method("test_#{action}_method_requires_fields") do
            		assert_nothing_raised do
						post action, options[:params],options[:session]
					end
            		for field in required_fields
      					assert !assigns(sym).errors[field].empty? unless options[:params][sym][field] == ''
    				end
    				assert !assigns.empty?,"This method requires specific parameters, yet no errors were created with them missing"
          		end
        	end
      	end
	end
	
	  #Expects format like this: 
	  #   test_required_unique_attributes_in_controller User,
	  #							{:params=>{:id=>1,:user=>{}},:session=>{:user=>{}}},
	  #							[:create, :update],
	  #							:username, :email_address
	  #
		
    def test_required_unique_attributes_in_controller( model, options, actions,*required_fields )
    	sym = model.to_s.downcase.to_sym
		for field in required_fields
      		options[:params][sym][field] = ''
    	end
		
      	actions.each do |action|
        	self.class_eval do
          		define_method("test_#{action}_method_requires_unique_fields") do
            		assert_nothing_raised do
						post action, options[:params],options[:session]
					end
            		for field in required_fields
      					assert !assigns(sym).errors[field].empty? unless options[:params][sym][field] == ''
    				end
    				assert !assigns.empty?,"This method fails without unique parameters, yet no errors were created"
          		end
        	end
      	end
    end
	
	#Expects format like this:
	# test_required_url_parameters_in_controller 
	#                                           User,
	#                                           {:params=>{},:session=>{:user=>{}}},
	#                                           :edit, :destroy, :show, :update	
	
    def test_required_url_parameters_in_controller(model, options, *actions)
      sym = model.to_s.downcase.to_sym
      actions.each do |action|
        self.class_eval do
          define_method("test_#{action}_method_requires_url_parameters") do
            post action, options[:params],options[:session]
            assert_response :redirect
            assert_nil assigns(sym),"This method requires a url parameter, yet the controller still created an object."
          end
        end
      end
    end
	
  end