rspec测试(使用guard自动测试和spork加速测试)配置

Gemfile文件添加rspec、guard和spork,之后执行bundle install命令

gem 'rb-readline'
group :development, :test do
  # Call 'byebug' anywhere in the code to stop execution and get a debugger console
  gem 'byebug'

  #rspec
  gem 'rspec-rails'

  #guard
  gem 'guard-rspec'
  gem 'selenium-webdriver'
  gem 'capybara'

  #spork
  gem 'spork-rails'
  gem 'guard-spork'
  gem 'childprocess'
end

引入rspec到rails工程中执行:rails g rspec:install

root@jec:~/RubymineProjects/Brush# rails g rspec:install
      create  .rspec
      create  spec
      create  spec/spec_helper.rb
      create  spec/rails_helper.rb

 

 

之后会在工程添加spec文件夹,如下图所示:

rspec已经引入成功,接下来初始化Guard,这样guard才能和rspec一起使用,执行:bundle exec guard init rspec

root@jec:~/RubymineProjects/Brush# bundle exec guard init rspec
15:15:51 - INFO - Writing new Guardfile to /root/RubymineProjects/Brush/Guardfile
15:15:51 - INFO - rspec guard added to Guardfile, feel free to edit it

运行guard:bundle exec guard

root@jec:~/RubymineProjects/Brush# bundle exec guard
19:09:06 - INFO - Guard::RSpec is running
19:09:06 - INFO - Guard is now watching at '/root/RubymineProjects/Brush'
[1] guard(main)> 

接下来导入Spork的设置:bundle exec spork --bootstrap

root@jec:~/RubymineProjects/Brush# bundle exec spork --bootstrap
Using RSpec, Rails
Bootstrapping /root/RubymineProjects/Brush/spec/spec_helper.rb.
Done. Edit /root/RubymineProjects/Brush/spec/spec_helper.rb now with your favorite text editor and follow the instructions.

修改spec/spec_helper.rb的rspec设置文件,让所需的环境在一个预派生(prefork)代码块中加载,保证环境只被加载一次

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'
# Requires supporting ruby files with custom matchers and macros, etc,
# in spec/support/ and its subdirectories.
  Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
# Checks for pending migrations before tests are run.
# If you are not using ActiveRecord, you can remove this line.
  ActiveRecord::Migration.check_pending! if defined?(ActiveRecord::Migration)
  RSpec.configure do |config|
# ## Mock Framework
#
# If you prefer to use mocha, flexmock or RR, uncomment the appropriate line:
#
# config.mock_with :mocha
# config.mock_with :flexmock
# config.mock_with :rr
# Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
    config.fixture_path = "#{::Rails.root}/spec/fixtures"
# If you're not using ActiveRecord, or you'd prefer not to run each of your
# examples within a transaction, remove the following line or assign false
# instead of true.
    config.use_transactional_fixtures = true
# If true, the base class of anonymous controllers will be inferred
# automatically. This will be the default behavior in future versions of
# rspec-rails.
    config.infer_base_class_for_anonymous_controllers = false
    # config.order = "random"
    config.include Capybara::DSL
  end
end

运行spork:bundle exec spork

#在启动spork之前,一定要先创建测试数据库,即执行rake db:create和rake db:migrate命令
root@jec:~/RubymineProjects/Brush# bundle exec spork
Using RSpec, Rails
Preloading Rails environment
Loading Spork.prefork block...
Spork is ready and listening on 8989!

 Guard和Spork协作,执行:bundle exec guard init spork

之后只要执行bundle exec guard就会自动启动spork服务器

root@jec:~/RubymineProjects/Brush# bundle exec guard init spork
19:21:28 - INFO - spork guard added to Guardfile, feel free to edit it

执行:bundle exec guard

root@jec:~/RubymineProjects/Brush# bundle exec guard
19:21:36 - INFO - Guard::RSpec is running
19:21:36 - INFO - Starting Spork for RSpec
Using RSpec, Rails
Preloading Rails environment
Loading Spork.prefork block...
Spork is ready and listening on 8989!
19:21:39 - INFO - Spork server for RSpec successfully started

19:21:39 - INFO - Guard is now watching at '/root/RubymineProjects/Brush'
[1] guard(main)> 

ok

你可能感兴趣的:(rspec测试(使用guard自动测试和spork加速测试)配置)