group :test do gem 'coveralls', require: false gem 'factory_girl_rails', :require => false gem 'faker' gem 'database_cleaner', '=1.0.1' # Clean database between tests gem 'rspec-rails' # Test framework gem 'shoulda' # nice rspec matchers end group :development, :test do gem 'fabrication' # Test object generation gem 'spring' gem 'guard-rspec'end
module Bbapi class Application < Rails::Applicationconfig.generators do |g| g.test_framework :rspec endend end
$ rails generate rspec:install
The generator creates a few files. Namely:.rspec - a config file where we can store extra command line options for the rspec command line tool. By default it contains --colour which turns on colored output from RSpec.spec - a directory that will store all of the various model, controller, view, acceptance and other specs for your appspec/spec_helper.rb - a file that's loaded by every spec (not in any automatic way but most have require 'spec_helper' at the top). It sets the test environment, contains app level RSpec configuration items, loads support files, and more.
rails generate rspec:model widget
will create a new spec file in spec/models/widget_spec.rb.
Other Rspec Generator :
it "is true when true" do
true.should be_true
end
to :
it "is true when true" do
expect(true).to be_true
end
1 aaron:
2 firstname: "Aaron"
3 lastname: "Sumner"
4 email: "aaron@everydayrails.com"
5
6 john:
7 firstname: "John"
8 lastname: "Doe"
9 email: "johndoe@nobody.org"
1 FactoryGirl.define do
2 factory :contact do
3 firstname "John"
4 lastname "Doe"
5 sequence(:email) { |n| "johndoe#{n}@example.com"}
6 end
7 end
example:
contact = FactoryGirl.build(:contact, firstname: nil)
1 RSpec.configure do |config|
2 # Include Factory Girl syntax to simplify calls to factories
3 config.include FactoryGirl::Syntax::Methods
4
5 end
Faker::Name.name #=> "Christophe Bartell"
Faker::Internet.email #=> "kirsten.greenholt@corkeryfisher.info"