RSpec : Conditionals in before/after callbacks

Conditions

When you add a conditions hash to before(:example) or before(:context), RSpec will only apply that hook to groups or examples that match the conditions. e.g.

RSpec.configure do |config|
  config.before(:example, :authorized => true) do
     :authorized_user
  end
end

describe Something, :authorized => true do
  # The before hook will run in before each example in this group.
end

describe SomethingElse do
  it "does something", :authorized => true do
    # The before hook will run before this example.
  end

  it "does something else" do
    # The hook will not run before this example.
  end
end

 

Or

config.before(:each) do |ex|
  if ex.metadata[:admin] == true
    Rails.application.load_seed # loading seeds
  end
end

it "should auto fill the shipping date field to today's date", :admin => true  do
  expect(User.find_by_email('user@example.com')).not_to eq(nil)
end

Sources

http://www.rubydoc.info/github/rspec/rspec-core/RSpec/Core/Hooks#before-instance_method

Ruby on Rails : GIT Precommit hooks Basics : Block debug command from commit

When you initialize git in your project you get a .git dir created in your project directory. The .git directoy contains a folder hooks along with other folders. Inside the hooks folder if you look you can see some files with prefix ‘sample’ in their names. The postfixes are the types of Hooks supported by git client. However, it may differ due to git version you are using.

What is Hook?

Hook is a piece of script with the help of which developer/programmer can customize the behavior of pre-packaged software. From the name of the hook we can normally anticipate at which point of execution the pre-packaged software execute the custom code.
Continue reading