BDD for Rails

安装组件

安装 RSpec

添加到Gemfile

group :development, :test do
  gem 'rspec-rails', '~> 3.0'
end

然后

$ bundle install

初始化RSpec目录

$ rails generate rspec:install

创建RSpec的binstub

$ bundle binstubs rspec-core

安装 Fuubar

fuubar 是一个使用进度条来表示RSpec测试进度的工具。
添加到Gemfile

group :test do
  gem 'fuubar'
end

运行

$ bundle install

将其设置为RSpec默认输出格式,编辑.rspec

—format Fuubar
—color

安装 shoulda-matchers

shoulda-matchers为RSpec扩充了很多rails内置方法的测试功能,不用自己重复写那些复杂的内置测试。
添加到Gemfile

group :test do
  gem 'shoulda-matchers'
end

运行

$ bundle install

安装 Factory Girl

Factory Girl是一个生成测试对象的工具
添加到Gemfile

group :development, :test do
  gem 'factory_girl_rails'
end

运行

$ bundle install

在RSpec配置文件spec_helper.rb添加配置

RSpec.configure do |config|
  .
  .
  .
  # Factory Girl

  config.include FactoryGirl::Syntax::Methods

end

在application.rb里添加下列配置

#add these lines

class Application < Rails::Application
    config.generators do |g|
      g.stylesheets false
      g.javascripts false
      g.test_framework :rspec,
        :fixtures => true,
        :view_specs => false,
        :helper_specs => false,
        :routing_specs => false,
        :controller_specs => true,
        :request_specs => true
      g.fixture_replacement :factory_girl, :dir => spec/factories
    end 
end

安装 Faker

faker 是一个生成测试数据的的工具, 如生成邮箱地址,手机号码,姓名
添加到Gemfile

group :test do
  gem 'faker'
end

运行

$ bundle install

安装 Database Cleaner

database cleaner可以确保每一次测试时,测试数据库都是干净的,没有干扰数据。
添加到Gemfile

group :test do
  gem 'database_cleaner'
end

运行

$ bundle install

给RSpec添加配置文件spec/spec_helper.rb

$ .
  .
  .
  RSpec.configure do |config|
  .
  .
  .
    # Database Cleaner

    config.before(:suite) do
      DatabaseCleaner.strategy = :transaction
      DatabaseCleaner.clean_with(:truncation)
    end
    config.before(:each) do
      DatabaseCleaner.start
      FactoryGirl.lint            # if FactoryGirl is installed !!!

    end
    config.after(:each) do
      DatabaseCleaner.clean
    end
  end

安装 Capybara-webkit

添加到Gemfile

group :test do
    gem capybara-webkit
end

运行

$ bundle install

在spec/spec_helper.rb里添加配置

.
.
require 'capybara/rspec'
.
Capybara.javascript_driver = :webkit
.
RSpec.configure do |config|
.

安装 spring

添加到Gemfile

group :development, :test do
  gem 'spring'
  gem 'spring-commands-rspec'
end

运行

$ bundle install
$ spring binstub —all

安装 SimpleCov

添加到Gemfile

group :test do
  gem 'simplecov', require: false
end

运行

$ bundle install

spec_helper.rb 第一行加载 simplecov

require 'simplecov' # if using simple simplecov

# see https://github.com/colszowka/simplecov#filters for filter/grouping etc

SimpleCov.start do  # for example…

  add_filter '/spec/'
  add_filter '/features/'
  add_filter '/config/'
  add_group 'Controllers', 'app/controllers'
  add_group 'Models', 'app/models'
  add_group 'Helpers', 'app/helpers'
  # add_group 'Views', 'app/views' # only .rb files are included at present!

  add_group 'Libraries', 'lib'
end
# Previous content of helper now start here

确保.gitignore里过滤下列目录

# Simplecov results
/coverage

安装guard

添加到Gemfile

group :development, :test do
  gem 'guard'
  gem 'guard-bundler'
  gem 'guard-rails'
  gem 'guard-rspec'
  gem 'guard-puma' # to have Guard use the Puma server rather than WEBrick

end

初始化Guardfile

$ guard init

安装完成

demo地址

comments powered by Disqus