The purpose of this project is to teach you how to use RSpec, a popular testing tool.
RSpec is testing tool for the Ruby programming language. Born under the banner of Behaviour-Driven Development, it is designed to make Test-Driven Development a productive and enjoyable experience.
This is a Rails project, by default it use Test::Unit and Fixture. However we use FactoryGirl to set up Ruby objects as test data. It support for multiple build strategies (saved instances, unsaved instances, attribute hashes, and stubbed objects), and support for multiple factories for the same class (user, admin_user, and so on), including factory inheritance.
If we want to use factory_girl with Rails, we need to include factory_girl_rails.
-
prepare database
You may change the database.yml setting first.
rake db:create rake db:migrate
-
add the following lines to your Gemfile
group :test, :development do gem "rspec-rails" gem "factory_girl" gem "factory_girl_rails" end Then run the following rake tasks: bunlde install rails generate rspec:install
-
run all tests
rake spec
-
run all modles tests
rake spec:models
-
run single test file
rspec spec/models/user_spec.rb
-
run single test method within a test file
rspec file_path:line_no The line_no is the method location in your test file. Such as: rspec spec/models/user_spec.rb:10