ROR 第三章 基本静态的页面(一)
新建项目:
cd ~/rails_projects rails new sample_app --skip-test-unit cd sample_app
添加gem:
group :development,:test do gem 'sqlite3' gem 'rspec-rails','2.13.1' end group :test do gem 'selenium-webdriver','2.0.0' gem 'capybara','2.1.0' end group :production do gem 'pg','0.15.1' end
安装包含gem
bundle install --without production
bundle update
bundle install
设置rails使用RSpec进行测试
rails generate rspec:install
git第一次初始化仓库
touch README.md
git init
git add README.md
git commit -m "first commit"
git remote add origin https://github.com/salembe/sample_app.git
git push -u origin master
以后只需要:
git remote add origin https://github.com/salembe/sample_app.git git push -u origin master
如果出现
error: The requested URL returned error: 403 while accessing https://github.com/salembe/sample_app.git/info/refs?service=git-receive-pack
fatal: HTTP request failed
不好意思,你被墙了。。。无解。。只能继续git push -u origin master 直到成功为止。
设置Rails,让其服务静态资源文件
config/environments/production.rb
config.serve_static_assets = true
推送到Heroku上,Heroku是一个免费的云空间,由于我之前没有用Heroku所以我先安装,然后登陆就可以了:
gem install heroku
heroku login
heroku keys:add //给heroku添加ssh key
附上Heroku的网站https://id.heroku.com/login,关于heroku与rails的关系可以参考这篇文章http://jhjguxin.hwcrazy.com/2012/02/01/heroku-rails-develop/
把项目简历到heroku上,并推送到git:
heroku create git push heroku master
3.1静态页面:
新建一个分支并切换:
git checkout -b static-pages
Rails提供了一个脚本generate来创建控制器,先生成home和help页面的控制器
--no-test-framework 禁用RSpec测试代码,晚一点会进行手动创建测试:
rails generate controller StaticPages home help --no-test-framework
然后接下来介绍了几种撤销的命令,方便以后出错的时候进行撤销操作,还是蛮重要的:
rails generate controller FooBars baz quux //生成控制器 rails destroy controller FooBars baz quux //撤销控制器 rails generate model Foo bar:string baz:integer //生成模型 rails destroy model Foo //撤销生成模型 rake db:migrate //迁移数据库 rake db:rollback //撤销迁移 rake db:migrate VERSION=0 //数据库回到原始状态 0换成其他数字,可以迁移到指定版本
提交一下内容到本地仓库,此时是在static-pages 分支下提交的:
git add . git commit -m "Add a StaticPages controller"
3.2 第一个测试
测试的过程“遇红,变绿,重构”。
首先生成集成测试:
rails generate integration_test static_pages
上面的代码会在spec/requests中生成static_pages_spec.rb 。
修改static_pages_spec.rb 文件内容为:
require 'spec_helper' describe "Static pages" do describe "Home page" do it "should have the content 'Sample App'" do visit '/static_pages/home' expect(page).to have_content('Sample App') end end end
为了让测试正确运行,需要在spec/spec_helper.rb中加入一行
. . . RSpec.config do |config| . . . config.include Capybara::DSL end
因为static_pages_spec.rb 中RSpec和Capybara使用的就是DSL语言。
执行rspec命令进行测试,为了保证RSpec运行在Gemfile指定的环境中还要加上
bundle exec:
bundle exec rspec spec/requests/static_pages_spec.rb
这个时候我电脑上出现了一个bug:
gems/selenium-webdriver-2.0.0/lib/selenium/webdriver/common/zipper.rb:1:in `require': cannot load such file -- zip/zip (LoadError)
修改Gemfile 'selenium-webdriver', '2.0.0
' to 'selenium-webdriver', '~> 2.35.1
'
bundle update 修复一下
再执行一次
bundle exec rspec spec/requests/static_pages_spec.rb
出现红色的Failure/Error 提示,表示测试成功,因为static_pages_spec.rb中测试需要home页面包含Sample App,而我们现在并没有包含,所以出错。
修改首页代码,让测试通过:
$vim app/views/static_pages/home.html.erb 修改内容
<h1>Sample App</h1> <p> This is the home page for the <a href="http://railstutorial.org/">Ruby on Rails Tutorial</a> sample application. </p>
再执行一次测试:
bundle exec rspec spec/requests/static_pages_spec.rb
出现绿色字体,测试通过。
帮助页面的也是同样测试:
$vim spec/requests/static_pages_spec.rb
require 'spec_helper' describe "Static pages" do describe "Home page" do it "should have the content 'Sample App'" do visit '/static_pages/home' expect(page).to have_content('Sample App') end end describe "Help page" do it "shou have the content 'Help'" do visit '/static_pages/help' expect(page).to have_content('Help') end end end
$bundle exec rspec spec/requests/static_pages_spec.rb
出现红色
修改Help页面代码,让测试通过:
$vim app/views/static_pages/help.html.erb 修改内容
<h1>Help</h1> <p> Get help on the Ruby on Rails Tutorial at the <a href="http://railstutorial.org/help">Rails Tutorial help page</a>. To get help on this sample app, see the <a href="http://railstutorial.org/book">Rails Tutorial book</a>. </p>
$bundle exec rspec spec/requests/static_pages_spec.rb
出现绿色,测试通过。
3.2.2 添加页面
添加测试about页面内容的代码
vim spec/requests/static_pages_spec.rb
require 'spec_helper' describe "Static pages" do describe "Home page" do it "should have the content 'Sample App'" do visit '/static_pages/home' expect(page).to have_content('Sample App') end end describe "Help page" do it "shou have the content 'Help'" do visit '/static_pages/help' expect(page).to have_content('Help') end end describe "About page" do it "should have the content 'About Us'" do visit '/static_pages/about' expect(page).to have_content('About Us') end end end
为about页面添加路由:
$vim config/routes.rb
SampleApp::Application.routes.draw do get "static_pages/home" get "static_pages/help" get "static_pages/about" . . . end
为about页面添加控制器:
$vim app/controllers/static_pages_controller.rb
class StaticPagesController < ApplicationController
def home
end
def help
end
def about
end
end
为about页面添加视图:
$ vim app/views/static_pages/about.html.erb
<h1>About Us</h1> <p> The <a href="http://railstutorial.org/">Ruby on Rails Tutorial</a> is a project to make a book and screencasts to teach web development with <a href="http://rubyonrails.org/">Ruby on Rails</a>.This is the sample application for the tutorial. </p>
$ bundle exec rspec spec/requests/static_pages_spec.rb
出现绿色,表示手工添加about页面,并且测试成功。