I tried it again based on my teammates automated testing summary. The following is a summary. You can take a look.
Note: the premise of this demo is that devise and rspec have been configured, and my blog posts in this regard are available for reference.
1. Install sport and Autotest-rails
Add in gemfile
Gem "spork", "~> 0.9.2"
Gem "Autotest-rails", "~> 4.1.2"
Execute bundle install
Install spork
Spork -- Bootstrap
Output:
Using rspec
Bootstrapping/home/Aaron/code/rspec_demo/spec/spec_helper.rb.
Done. Edit/home/Aaron/code/rspec_demo/spec/spec_helper.rb now with your favorite text editor and follow the instructions.
The above is to modify the spec/spec_helper.rb file and add two methods in its header. Spork. prefork (executed only once at startup) and spork. each_run (executed every time called by rspec ).
2. Modify the rspec configuration to implement interaction between rspec and spork
Require 'rubygems'
Require 'spork'
# Uncomment the following line to use spork with the debugger
# Require 'spork/EXT/ruby-debug'
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.
# This file is copied to spec/when you run 'rails generate rspec: install'
Env ["rails_env"] | = 'test'
Require file. expand_path ("http://www.cnblogs.com/config/environment", _ file __)
Require 'rspec/rails'
Require 'rspec/Autorun'
# 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}
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 = false
# If true, the base class of anonymous controllers will be inferred
# Automatically. This will be the default behavior in future versions
# Rspec-rails.
Config. infer_base_class_for_anonymous_controllers = false
# Run specs in random order to surface order dependencies. If you find
# Order dependency and want to debug it, you can fix the order by providing
# The seed, which is printed after each run.
# -- Seed 1234
Config. Order = "random"
End
Spork. each_run do
# This code will be run each time you run your specs.
Load "# {rails. Root}/config/routes. RB"
Dir ["# {rails. Root}/APP/**. RB"]. Each {| f | load f}
End
# --- Instructions ---
# Sort the contents of this file into a spork. prefork and a spork. each_run
# Block.
#
# The spork. prefork block is run only once when the spork server is started.
# You typically want to place most of your (slow) initializer code in here, in
# Particle, require 'ing any 3rd-party gems that you don't normally modify
# During development.
#
# The spork. each_run block is run each time you run your specs. In case you
# Need to load files that tend to change during development, require them here.
# With rails, your application modules are loaded automatically, so sometimes
# This block can remain empty.
#
# Note: you can modify files loaded * from * The spork. each_run block
# Restarting the spork server. However, this file itself will not be reloaded,
# So if you change any of the code inside the each_run block, you still need
# Restart the server. In general, if you have non-trivial code in this file,
# It's advisable to move it into a separate file so you can easily edit it
# Without restarting spork. (for example, with rspec, you cocould move
# Non-trivial code into a file spec/support/my_helper.rb, making sure that
# Spec/support/* files are require 'd from inside the each_run block .)
#
# Any code that is left outside the two blocks will be run during preforking
# * And * During each_run -- that's probably not what you want.
#
# These Instructions shoshould self-destruct in 10 seconds. If they don't, feel
# Free to delete them.
End
Iii. Tell rspec that we use spork
Modify the rspec configuration file (your_app/. rspec) and tell it that we use spork to add code:
--drb
Generally, the complete code after modification is:
--colour--drb
4. Now rspec and spork can be perfectly combined.
Start spork Method
Run rspec
The rspec execution time is significantly shortened. However, if you run "rake" to start rspec, it will still be slow because rake will load the project environment at startup to prepare for all the tasks.
5. Add Autotest to automatically execute the test
Start Autotest
Autotest is automatically started and monitored for file changes. Once changed, the test is executed immediately.
It is easiest to add Autotest because some support for Autotest has been added to rspec, which is also recommended by rspec rails.
There are also some problems with simplicity, such as lack of Autotest customization and flexibility. If you are pursuing customization, use guard.
Vi. Note
When you modify APP/models/person. RB, the rspec test may not be executed automatically because the test environment of rails caches the class by default.
Modify "config/environments/test. RB"
123 |
#config/environments/test.rb #config.cache_classes = true #change this line to config.cache_classes = false |
VII. References
References
A summary of how factory girl works: http://rubyer.me/blog/1460/
Devise: https://github.com/plataformatec/devise
Rspecrails: https://github.com/rspec/rspec-rails
Rails3-Devise-Rspec-cucumber example: https://github.com/RailsApps/rails3-devise-rspec-cucumber
Rspec + spork + Autotest: http://rubyer.me/blog/1477/