Micropost refinements: Construct some microposts in the User model test using FactoryGirl

Technique:
We tell Factory Girl about the micropost’s associated user just by including a user in the definition of the factory
factory :micropost do
  content "Lorem ipsum"
  user
end

this allows us to define factory microposts as follows:

FactoryGirl.create(:micropost, user: @user, created_at: 1.day.ago)
Micropost refinements: A failing test for the order of a user's microposts

Technical:
We use 'let!' instead of 'let' because 'let' is lazy, variables spring into existence when referenced, while 'let!' create the variables immediately.
Micropost refinements: Ordering the microposts with 'default_scope'

Technicals:
1. A Rails facility called 'default_scope' with an 'order' argument
2. Use of Ruby Lambda expression
As of Rails 4.0, all scopes take an anonymous function that returns the criteria desired for the scope, mainly so that the scope need not be evaluated immediately, but rather can be loaded later on an as-needed basis (so-called lazy evaluation)
To understand the difference: In an early revision of the Rails Tutorial book 2nd edition, the code is:
default_scope order: 'microposts.created_at DESC'
instead of:
default_scope -> { order('created_at DESC') }

Lambda expression (Ruby syntax)
In Ruby, lambda is used to denote anonymous functions or closures
It is an operator which take a block and return an object of class Proc (procedure)
Ruby has alternative ways of creating Proc objects. Rather than write this:
  lambda { |params| ... }
you can also write the following:
  -> params { ... }
So '-> { order('created_at DESC') }' equals to 'lambda { order('created_at DESC') }'

Proc object can be called like this:
p1 = lambda {|n| 23 * n}
p1.call(3) # => 69
Micropost refinements: A failing test that when user is deleted, his microposts are deleted as well

Techniques:
1. microposts = @user.microposts.to_a
Here the call to to_a effectively makes a copy of the microposts. Without to_a, destroying the user would destroy the posts in the microposts variable, and as a result,
  microposts.each do |micropost|
    # Make sure the micropost doesn't appear in the database.
  end
wouldn't test anything because microposts would be empty.

2. Used Micropost.where instead of Micropost.find, because 'find' raises an exception if the record is not found, while 'where' returns an empty object.
Alternatively, you can use 'find_by_id' method which returns nil if record is not found
Or if you use 'find', you should write:
  lambda do // lambda again
    Micropost.find(micropost.id)
  end.should raise_error(ActiveRecord::RecordNotFound)
Or:
  expect do
    Micropost.find(micropost)
  end.to raise_error(ActiveRecord::RecordNotFound)
Micropost refinements: Add 'dependent: :destroy' option to implement cascade delete