Last active
August 29, 2015 14:18
-
-
Save jose8a/a5ce9014c1f021a41391 to your computer and use it in GitHub Desktop.
Guide for setting up TDD, BDD, or Unit testing in a rails app ... primarily for setting up tests with Rails App Template
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
##Resources | |
https://semaphoreci.com/community/tutorials/setting-up-the-bdd-stack-on-a-new-rails-4-application | |
https://robots.thoughtbot.com/how-we-test-rails-applications | |
http://devblog.avdi.org/2012/08/31/configuring-database_cleaner-with-rails-rspec-capybara-and-selenium/ | |
## Install Rails and the default gems | |
####Generate new rails app w/out default test and without default 'bundle'ing | |
rails new --skip-test-unit --skip-bundle myapp | |
####Manually install gems locally into per-project bundle | |
cd myapp | |
bundle install --path vendor/bundle | |
##Now Install RSpec | |
group :development, :test do | |
gem 'rspec-rails' | |
end | |
####Install the rspec gem | |
bundle | |
####Bootstrap the application with RSpec | |
bundle exec rails generate rspec:install | |
##Install Shoulda-matchers | |
##Install factory_girl | |
##Make sure everything is connected and working | |
bundle exec rails generate model Post title:string content:text | |
bundle exec rake db:migrate | |
####Define a Post factory: | |
``` | |
# spec/factories/posts.rb | |
FactoryGirl.define do | |
factory :post do | |
title "My first post" | |
content "Hello, behavior-driven development world!" | |
end | |
end | |
``` | |
``` | |
####Update spec to validate Post's content & title | |
# spec/models/post_spec.rb | |
require 'rails_helper' | |
RSpec.describe Post, type: :model do | |
it { is_expected.to validate_presence_of(:title) } | |
it { is_expected.to validate_length_of(:title).is_at_least(5) } | |
it { is_expected.to validate_presence_of(:content) } | |
it { is_expected.to validate_length_of(:content).is_at_least(10) } | |
end | |
``` | |
``` | |
# app/models/post.rb | |
class Post < ActiveRecord::Base | |
validates :title, presence: true, length: { minimum: 5 } | |
validates :content, presence: true, length: { minimum: 10 } | |
end | |
``` | |
####Run Post specs | |
bundle exec rspec spec/models/post_spec.rb |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment