Skip to content

Commit

Permalink
improving specs
Browse files Browse the repository at this point in the history
  • Loading branch information
tarasevich committed Apr 14, 2011
1 parent 3a4018f commit 6a964e6
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 21 deletions.
4 changes: 2 additions & 2 deletions spec/active_factory_define_spec.rb
Expand Up @@ -59,8 +59,8 @@ def get_factory name
{:text => "Post with after_build"}

post = mock "Post"
post.should_receive(:text=).with("After Build").and_return(nil)
factory.apply_after_build nil, nil, post
post.should_receive(:text=).with("After Build 0").and_return(nil)
factory.apply_after_build 0, nil, post
end

it "with preferred associations" do
Expand Down
52 changes: 36 additions & 16 deletions spec/active_factory_spec.rb
Expand Up @@ -14,7 +14,20 @@

# INTRODUCING METHODS

it "defines methods to access singleton object, singleton hash, collection and collection of hashes" do
it "defines methods in models{} section to declare a singleton object and hash, collection of objects and hashes" do
models {
respond_to?(:user).should == true
respond_to?(:user_).should == true
respond_to?(:users).should == true
respond_to?(:users_).should == true

respond_to?(:simple_user).should == true
respond_to?(:follower).should == true
respond_to?(:post).should == true
}
end

it "defines methods in the current spec context to access a singleton object, a singleton hash, a collection and a collection of hashes" do
models { user }

respond_to?(:user).should == true
Expand Down Expand Up @@ -154,7 +167,7 @@
models { post - user }

post.user.should == user
user.posts.should == post
user.posts.should == [post]

Post.all.should == [post]
Post.all.map(&:user).should == [user]
Expand All @@ -164,7 +177,7 @@
models { user - post }

post.user.should == user
user.posts.should == post
user.posts.should == [post]

User.all.should == [user]
User.all.map(&:posts).should == [[post]]
Expand All @@ -177,19 +190,15 @@
posts.map(&:user).should == [user, user]

User.all.should == [user]
User.all.map(&:posts).should == [posts]
User.all.map(&:posts).should =~ [posts]
end

it "associates collections" do
models { posts(3) - users(1) }

posts = 3.times.map { |n| Post.find_by_text("Content #{n}") }
_posts = 3.times.map { |n| Post.find_by_text("Content #{n}") }

1.times { |n|
user = User.find_by_email("user#{n}@tut.by")
assert user
user.posts.all.should == posts
}
user.posts.all.should == _posts
end

it "zips links" do
Expand All @@ -198,33 +207,44 @@
2.times { |i|
posts[i].user_id.should == users[i].id
}

Post.all.should =~ posts
Post.all.map(&:user).should =~ users
end

it "it associates only specified number of posts, not all" do
models { posts(1); posts(1) - user } #.define_all
models { posts(1); posts(1) - user }

assert posts[1].user
posts[0].user.should == nil
posts[1].user.should == user

Post.all.map(&:user).should =~ [nil, user]
end

# ADVANCED FACTORY OPTIONS

it "uses prefer_associations option" do
it "uses prefer_associations option to disambiguate associations (followers & following)" do
models { follower - user }

follower.following.should == [user]
follower.followers.should == []

User.all.should =~ [follower, user]
end

it "allows to redefine prefer_associations" do
models { follower - :followers - user }

follower.followers.should == [user]
follower.following.should == []

User.all.should =~ [follower, user]
end

it "invokes after_create" do
models { post_with_after_build } #.define_all
it "invokes after_build" do
models { post_with_after_build }

post_with_after_build.text.should == "After Build"
post_with_after_build.text.should == "After Build 0"
end

it "provides methods for hash keys" do
Expand Down
2 changes: 1 addition & 1 deletion spec/define_lib_factories.rb
Expand Up @@ -15,7 +15,7 @@ class ActiveFactory::Define

factory :post_with_after_build, :class => Post do
text "Post with after_build"
after_build { object.text = "After Build" }
after_build { object.text = "After Build #{i}" }
end

factory :post_overrides_method, :class => Post do
Expand Down
6 changes: 4 additions & 2 deletions spec/support/active_record_environment.rb
Expand Up @@ -24,8 +24,10 @@

class User < ActiveRecord::Base
has_many :posts
has_and_belongs_to_many :followers, :class_name => 'User', :foreign_key => 'following_id', :join_table => 'following'
has_and_belongs_to_many :following, :class_name => 'User', :foreign_key => 'follower_id', :join_table => 'following'
has_and_belongs_to_many :followers, :class_name => 'User', :join_table => 'following',
:foreign_key => 'following_id', :association_foreign_key => 'follower_id'
has_and_belongs_to_many :following, :class_name => 'User', :join_table => 'following',
:foreign_key => 'follower_id', :association_foreign_key => 'following_id'
attr_accessor :password
end

Expand Down

0 comments on commit 6a964e6

Please sign in to comment.