From 84ce937e50c28a64da3e77f609372b6e0171fcef Mon Sep 17 00:00:00 2001 From: nathanvda Date: Sun, 6 Mar 2011 22:40:37 +0100 Subject: [PATCH] Added tests for link_to_add_association. --- spec/cocoon_spec.rb | 22 +++++++++++++- spec/dummy/app/models/comment.rb | 3 ++ spec/dummy/app/models/post.rb | 3 ++ .../db/migrate/20110306212208_create_posts.rb | 14 +++++++++ .../migrate/20110306212250_create_comments.rb | 15 ++++++++++ spec/dummy/db/schema.rb | 30 +++++++++++++++++++ 6 files changed, 86 insertions(+), 1 deletion(-) create mode 100644 spec/dummy/app/models/comment.rb create mode 100644 spec/dummy/app/models/post.rb create mode 100644 spec/dummy/db/migrate/20110306212208_create_posts.rb create mode 100644 spec/dummy/db/migrate/20110306212250_create_comments.rb create mode 100644 spec/dummy/db/schema.rb diff --git a/spec/cocoon_spec.rb b/spec/cocoon_spec.rb index 196cf64..809d590 100644 --- a/spec/cocoon_spec.rb +++ b/spec/cocoon_spec.rb @@ -1,7 +1,6 @@ require 'spec_helper' describe Cocoon do - class TestClass < ActionView::Base end @@ -11,4 +10,25 @@ describe Cocoon do it { should respond_to(:link_to_add_association) } it { should respond_to(:link_to_remove_association) } + context "link_to_add_association" do + before(:each) do + @tester = TestClass.new + @post = Post.new + @form_obj = stub(:object => @post) + @tester.stub(:render_association).and_return('form') + end + + it "should accept a name without a block" do + result = @tester.link_to_add_association('add something', @form_obj, :comments) + result.to_s.should == 'add something' + end + + it "should work with a block" do + result = @tester.link_to_add_association(@form_obj, :comments) do + "some long name" + end + result.to_s.should == 'some long name' + end + end + end \ No newline at end of file diff --git a/spec/dummy/app/models/comment.rb b/spec/dummy/app/models/comment.rb new file mode 100644 index 0000000..4e76c5b --- /dev/null +++ b/spec/dummy/app/models/comment.rb @@ -0,0 +1,3 @@ +class Comment < ActiveRecord::Base + belongs_to :post +end diff --git a/spec/dummy/app/models/post.rb b/spec/dummy/app/models/post.rb new file mode 100644 index 0000000..6ad2382 --- /dev/null +++ b/spec/dummy/app/models/post.rb @@ -0,0 +1,3 @@ +class Post < ActiveRecord::Base + has_many :comments +end diff --git a/spec/dummy/db/migrate/20110306212208_create_posts.rb b/spec/dummy/db/migrate/20110306212208_create_posts.rb new file mode 100644 index 0000000..66f0afa --- /dev/null +++ b/spec/dummy/db/migrate/20110306212208_create_posts.rb @@ -0,0 +1,14 @@ +class CreatePosts < ActiveRecord::Migration + def self.up + create_table :posts do |t| + t.string :title + t.text :body + + t.timestamps + end + end + + def self.down + drop_table :posts + end +end diff --git a/spec/dummy/db/migrate/20110306212250_create_comments.rb b/spec/dummy/db/migrate/20110306212250_create_comments.rb new file mode 100644 index 0000000..ddb296a --- /dev/null +++ b/spec/dummy/db/migrate/20110306212250_create_comments.rb @@ -0,0 +1,15 @@ +class CreateComments < ActiveRecord::Migration + def self.up + create_table :comments do |t| + t.text :body + t.string :author + t.integer :post_id + + t.timestamps + end + end + + def self.down + drop_table :comments + end +end diff --git a/spec/dummy/db/schema.rb b/spec/dummy/db/schema.rb new file mode 100644 index 0000000..3c796af --- /dev/null +++ b/spec/dummy/db/schema.rb @@ -0,0 +1,30 @@ +# This file is auto-generated from the current state of the database. Instead +# of editing this file, please use the migrations feature of Active Record to +# incrementally modify your database, and then regenerate this schema definition. +# +# Note that this schema.rb definition is the authoritative source for your +# database schema. If you need to create the application database on another +# system, you should be using db:schema:load, not running all the migrations +# from scratch. The latter is a flawed and unsustainable approach (the more migrations +# you'll amass, the slower it'll run and the greater likelihood for issues). +# +# It's strongly recommended to check this file into your version control system. + +ActiveRecord::Schema.define(:version => 20110306212250) do + + create_table "comments", :force => true do |t| + t.text "body" + t.string "author" + t.integer "post_id" + t.datetime "created_at" + t.datetime "updated_at" + end + + create_table "posts", :force => true do |t| + t.string "title" + t.text "body" + t.datetime "created_at" + t.datetime "updated_at" + end + +end