Rspec非常的强大,一旦用上,在也舍不得换了。 不过每次集成Rspec都比较麻烦,因为相关的Gem比较多,而且需要修改一些配置文件。本文记录下我集成Rspec集成过程。
- 增加Gem
group :development, :test do gem 'database_cleaner' gem 'factory_girl' gem 'factory_girl_rails' gem 'rspec-rails', '2.9.0' gem 'capybara', '1.1.2' end
运行命令
bundle install
- 生成spec_helper.rb文件
rails g rspec:install
- 修改spec_helper.rb文件
对capybara和databaseclean的支持ENV["RAILS_ENV"] ||= 'test' require File.expand_path("../../config/environment", __FILE__) require 'rspec/rails' require 'rspec/autorun' require 'capybara/rspec' Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f} RSpec.configure do |config| config.mock_with :rspec DatabaseCleaner.strategy = :truncation config.before do DatabaseCleaner.clean end end
- 然后在增加了 integeration 测试 一下吧
rails g rspec:integration create_interface
- 运行命令
bundle exec rspec spec/requests/create_interfaces_spec.rb
- 修改一下代码,在测试
describe "CreateInterfaces" do describe "GET root_url" do it "visit root_url" do visit root_url end end end
- 基本上已经可以跑同了,但是还有很多其他的问题。比如没有spork 和 guard的支持,下次总结。
###############################################z
- 增加spork的支持
gem 'spork', '~> 1.0rc'
运行命令
spork rspec --bootstrap
修改spec_helper.rb,如
require 'rubygems' require 'spork' Spork.prefork do # Loading more in this block will cause your tests to run faster. However, # if you change any configuration or code from libraries loaded here, you'll # need to restart spork for it take effect. ENV["RAILS_ENV"] ||= 'test' require File.expand_path("../../config/environment", __FILE__) require 'rspec/rails' require 'rspec/autorun' require 'capybara/rspec' Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f} RSpec.configure do |config| config.mock_with :rspec config.include Devise::TestHelpers, :type => :controller DatabaseCleaner.strategy = :truncation config.before do DatabaseCleaner.clean end end end Spork.each_run do # This code will be run each time you run your specs. require 'rspec/rails' Dir["#{Rails.root}/app/controllers//*.rb"].each do |controller| load controller end Dir["#{Rails.root}/app/models//*.rb"].each do |model| load model end Dir["#{Rails.root}/app/models/ext_model//*.rb"].each do |model| load model end FactoryGirl.reload end
修改.rspec文件
--drb
使用如下命令测试
bundle exec spork # run spork! rake spec # run all spec test
- 增加guard支持
gem 'guard' gem 'guard-spork', '0.3.2' gem 'guard-rspec'
- 生成Guard配置文件
guard init
修改配置文件
guard 'rspec', :version => 2, :all_on_start => false, :all_after_pass => false do ... ... end
首先启动,不 运行所有的测试例子, 测试成功后,不运行所有的测试用列