页码为《Ruby on Rails Tutorial 2nd Edition》
创建controller:P86
rails generate controller StaticPages home help --no-test-framework
之后,创建integration_test
rails generate integration_test XXX
创建model:
rails generate model name:string email:string
将更改反映到数据库:
bundle exec rake db:migrate
准备test数据库:
bundle exec rake db:test:prepare(若test database奇怪地出错,建议使用该命令)
运行rspec测试:
bundle exec rspec ____(文件位置,也可为文件夹)
运行annotation(为model自动创建数据库表结构注释) P229
bundle exec annotate --position before
运行自动测试框架guard:
1. 将guard加入gemfile
2. bundle install
3. bundle exec guard init rspec使其与rspec集成
4. 更改guardfile,guard 'rspec', :version => 2, :all after pass => false, :cli => '--drb' do
5. 运行guard: bundle exec guard
使用spork加速rspec(因为rspec每次运行都会调入自身环境,浪费时间)
1. 加入gemfile: gem 'guard-spork', '0.3.2' gem 'spork', '0.9.0'
2. bundle install
3. bundle exec spork --bootstrap
4. 更改spec_helper.rb:
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("../../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
config.mock with :rspec
# 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
end
end
5. bundle exec spork
6. 更改.spec文件,添加--drb行(distributed ruby)
7. 与guard集成: bundle exec guard init spork
进行数据库更改:
rails generate migration add_index_to_users_email 并自己编辑相关更改
bundle exec rake db:migrate 应用更改
bundle exec rake db:test:prepare 准备测试数据库
bundle exec rake db:migrate RAILS_ENV=production 创建production数据库
bundle exec rake db:reset 重置所有数据库
bundle exec rake db:rollback 撤销更改