Skip to content

Instantly share code, notes, and snippets.

@randalmaile
Last active January 2, 2016 13:09
Show Gist options
  • Save randalmaile/8308029 to your computer and use it in GitHub Desktop.
Save randalmaile/8308029 to your computer and use it in GitHub Desktop.
Bloccit - Testing Users
1. In the post.rb, comment.rb factory classes, how come the empty attributes don't matter. Seems like black magic. I thought you would need the created objects to be valid?
I don't understand the use of attributes_for(:post), for instance, in the post example. Does this automatically set dummy values for all the attributes, or does it just call the attributes from the :post factory?
2. Not really understanding this part:
```
This is a FactoryGirl method. -- You can learn more by referring to the Getting Started documentation. It will return a hash of arguments for your model so you can use the hash in a create method. It does not include relationships in the return hash. You will be using attributes_for when you build comments and posts. It will enable you to assign an associated user object to a newly created post or comment. Here is an example of how you can create a new user with attributes_for:
@user = create(:user) do |user|
post = user.posts.build(attributes_for(:post))
post.topic = create(:topic)
post.save
c = user.comments.build(attributes_for(:comment))
c.post = post
c.save
end
```
I know there's a reason for using build and attributes_for(:post) but not getting it.
1. When dealing with multiple instances of a class, or trying to grab all the instances of a class, (as with top_rated) it's important to ensure the database is properly cleaned between tests. We'll use the database_cleaner for that.
2.
@mkwiatkowski
Copy link

http://refactoring.com/catalog/extractMethod.html

require 'spec_helper'

describe User do

  describe ".top_rated" do
    before :each do
      post = nil
      topic = create(:topic)
      @u0 = create(:user) do |user|
        post = user.posts.build(attributes_for(:post))
        post.topic = topic
        post.save
        c = user.comments.build(attributes_for(:comment))
        c.post = post
        c.save
      end

      @u1 = create(:user) do |user|
        make_comment(user)
        make_post(user)
        make_comment(user)
      end
    end

    it "should return users based on comments + posts" do
      User.top_rated.should eq([@u1, @u0])
    end
    it "should have `posts_count` on user" do
      users = User.top_rated
      users.first.posts_count.should eq(1)
    end
    it "should have `comments_count` on user" do
      users = User.top_rated
      users.first.comments_count.should eq(2)
    end
  end

  def make_comment(user, n=1)
    n.times do
      c = user.comments.build(attributes_for(:comment))
      c.post = post
      c.save
    end
  end

  def make_post(user)
    post = user.posts.build(attributes_for(:post))
    post.topic = topic
    post.save
  end
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment