require File.join(File.dirname(__FILE__), 'test_helper') class ActsAsJoinableTest < ActiveSupport::TestCase context "ActsAsJoinable" do setup do create_models(1, 2) end should "have 1 of each post, asset, tag to start" do assert_equal 1, Post.count assert_equal 2, Tag.count assert_equal 2, Asset.count end should "have the correct number of Relationship models" do result = [ActsAsJoinable::Relationship.find_all_by_parent_type("Post")] result << ActsAsJoinable::Relationship.find_all_by_parent_type("Asset") result << ActsAsJoinable::Relationship.find_all_by_parent_type("Tag") result.each do |kind| kind.sort {|a, b| b.child_type <=> a.child_type}.each do |relationship| end end end context "correct generated methods" do setup do @post = Post.first @asset = Asset.first @tag = Tag.first end should "Post should respond_to?(:tags) and respond_to?(:assets)" do assert @post.respond_to?(:tags) assert @post.respond_to?(:assets) end should "Tag should respond_to?(:posts) and respond_to?(:assets)" do assert @tag.respond_to?(:posts) assert @tag.respond_to?(:assets) end should "Post have tags and assets" do assert_equal 2, @post.tags.count assert_equal 2, @post.assets.count end should "Tag have Post and Asset" do assert_equal 1, @tag.posts.count assert_equal 1, @tag.assets.count assert_equal 1, Tag.first.posts.count assert_equal 1, Tag.first.assets.count end should "Asset have Post and Tags" do assert_equal 1, @asset.tags.count assert_equal 1, @asset.posts.count assert_equal 1, Asset.first.tags.count assert_equal 1, Asset.first.posts.count end context "custom scopes" do should "have a 'featured_assets' scope on Post" do @post = Post.first assert @post.respond_to?(:featured_assets) @featured = Asset.first @post.featured_assets << @featured #@post.add_featured_asset(@featured) @post = Post.first assert_equal @post.featured_assets.first, @featured assert @post.assets.include?(@featured) end end end context "manipulating posts" do setup do @post = Post.create!(:title => "testing") end should "be able to add and remove objects" do @post.assets << Asset.all @post.featured_assets << Asset.all @post = Post.last end end teardown do destroy_models end end end