ENV["RAILS_ENV"] = "test" require File.expand_path(File.dirname(__FILE__) + "/../config/environment") require 'test_help' require File.dirname(__FILE__) + '/unit_test_assistant' #? class Test::Unit::TestCase # Transactional fixtures accelerate your tests by wrapping each test method # in a transaction that's rolled back on completion. This ensures that the # test database remains unchanged so your fixtures don't have to be reloaded # between every test method. Fewer database queries means faster tests. # # Read Mike Clark's excellent walkthrough at # http://clarkware.com/cgi/blosxom/2005/10/24#Rails10FastTesting # # Every Active Record database supports transactions except MyISAM tables # in MySQL. Turn off transactional fixtures in this case; however, if you # don't care one way or the other, switching from MyISAM to InnoDB tables # is recommended. self.use_transactional_fixtures = false # Instantiated fixtures are slow, but give you @david where otherwise you # would need people(:david). If you don't want to migrate your existing # test cases which use the @david style and don't mind the speed hit (each # instantiated fixtures translates to a database query per test method), # then set this back to true. self.use_instantiated_fixtures = true # default: false # Add more helper methods to be used by all tests here... # Shorthand method to get a DB connection in test def db ActiveRecord::Base.connection end def setup_forum_fixture create_fixtures 'forums', 'posts', 'topics', 'users', 'topic_reads', 'tags', 'topics_tags' end FtaPrototypes::set_prototype :Forum, { 'name' => 'Prototype Forum', 'description' => 'Prototype Forum Description', 'created_at' => Time.now } FtaPrototypes::set_prototype :Post, { 'topic_id' => 1, 'l' => 1, 'r' => 1, 'subject' => 'Prototype Post Subject', 'text' => 'Prototype Post Text', 'messageid' => "prototype@#{RForum::CONFIG[:hostname]}" } FtaPrototypes::set_prototype :Topic, { 'forum_id' => 1, 'subject' => 'Prototype Topic Subject', 'last_post_created_at' => Time.at(946702800) } FtaPrototypes::set_prototype :User, { 'name' => 'ryan', 'firstname' => 'ryan', 'surname' => 'platte', 'email' => 'ryan.platte@example.com' } # Asserts that all messages in a topic belong to the same nested set, and # the nested set is well formed def assert_nested_set_well_formed(topic_id) posts = Post.find_all("topic_id = #{topic_id}", 'l ASC') return if posts.empty? num_posts = posts.size # Check the topmost post top_post = posts.shift assert_equal 1, top_post.l assert_equal num_posts * 2, top_post.r # Check all other posts prev = top_post posts.each do |cur| assert cur.l > top_post.l assert cur.r > top_post.l assert cur.l < top_post.r assert cur.r < top_post.r if cur.children_count == 0 assert_equal cur.l + 1, cur.r end if prev.id == cur.parent_id assert_equal prev.l + 1, cur.l assert prev.r > cur.r end prev = cur end end end class ActiveRecord::Errors def to_s s = "Problems with #{@base.class} ##{@base.id || 'nil'} :\n" self.each { |attr, msg| s << " #{attr} : #{msg} (value: #{@base[attr.to_s] || 'nil'})\n" } s end end # setup_class_controller method class Test::Unit::TestCase private def setup_controller_test(controller_class = nil, host = nil) if controller_class @controller = controller_class.new elsif self.class.to_s =~ /(\w+Controller)Test/ @controller = Object::const_get($1).new else raise "Cannot derive the name of controller under test from class name #{self.class}" end @request, @response = ActionController::TestRequest.new, ActionController::TestResponse.new @request.host = host || 'localhost' return @request, @response end end