# TestHelpful class Test::Unit::TestCase include ActionController::PolymorphicRoutes include ActionController::RecordIdentifier def assert_form(instance, *attrs) name = singular_class_name(instance) assert_select("form[action=#{polymorphic_path(instance)}]") do attrs.each do |attr| assert_select("##{name}_#{attr}[name=?]", "#{name}[#{attr}]") end assert_select("input[type=submit]") end end def create_by_model(type, default_options, options = { }) type.create(default_options.merge(options)) end def create_by_controller(type, default_options, options = { }) post :create, type.to_sym => default_options.merge(options) end def model_name classname = self.class.name classname.gsub!(/ControllerTest/, "") classname.gsub!(/Test/, "") classname.singularize end def create(options = { }) attr_var = eval("@@#{model_name.downcase}_default_attr ||= {}") if self.class.name =~ /Controller/ create_by_controller(model_name.downcase, attr_var, options) assigns(model_name.downcase.to_sym) else create_by_model(eval(model_name), attr_var,options) end end def assert_errors_on(model, *attrs) attrs.each do |attr| assert model.errors.on(attr), "#{attr} has no error" end end # Sets the current user in the session from the user fixtures. def login_as(user) @request.session[:user] = user ? users(user).id : nil end end