來自 RailsTutorial 3rd 第三章javascript
rails new demo3-simpleapp --skip-bundle # 可更改gem源到國內 https://ruby.taobao.org # 添加gem包 bundle install --without production
git init git add -A git commit -m "init simple app" git remote add origin git@bitbucket.org:imedingyiming/demo3-simpleapp.git git push -u origin --all
heroku create demo3-simpleapp git push heroku master heroku logs heroku open
git checkout master git checkout -b static-pages
rails g controller StaticPages home help
rails server rails s rails console rails c rails generate rails g bundle install bundle rake test rake
git status git add -A git commit -m "add static pages" git push -u origin static-pages
Ruby文件名通常使用蛇底式,rails生成器使用
underscore
方法把駝峯式轉換成蛇底式html
rails destroy controller StaticPages home help rails destroy model User bundle exec rake db:rollback bundle exec rake db:migrate VERSION=0
好處java
1.測試避免"迴歸",即因爲某些緣由以前能用的功能不能用了;git
2.有測試,重構(改變實現方式,但功能不變)時更有自信;安全
3.測試是應用代碼的客戶ruby
何時進行測試app
和應用代碼相比,測試代碼特別簡短,傾向於先編寫測試ide
若是實現的功能不清楚,傾向於先寫應用代碼佈局
爲安全先測試測試
只要發現一個問題,就編寫一個測試重現這個問題,以免迴歸,而後再編寫應用代碼修正問題
儘可能不爲之後可能修改的代碼編寫測試
重構以前要編寫測試,集中測試容易出錯的代碼
通常先編寫控制器和模型測試,再編寫集成測試,若是代碼常常變更就徹底不測試
ls test/controllers/ static_pages_controller_test.rb # test/controllers/static_pages_controller_test.rb require 'test_helper' class StaticPagesControllerTest < ActionController::TestCase test "should get home" do get :home assert_response :success end test "should get help" do get :help assert_response :success end end # 執行測試 bundle exec rake test
RED -> GREEN -> REWORK
1.RED
#添加 test "should get home" do get :home assert_response :success assert_select "title","Home | Ruby on Rails Tutorial" end test "should get help" do get :help assert_response :success assert_select "title","Help | Ruby on Rails Tutorial" end test "should get about" do get :about assert_response :success assert_select "title","About | Ruby on Rails Tutorial" #html標籤爲何內容 end
測試:bundle exec rake test
2.GREEN
#routes.rb get 'static_pages/about' #static_pages_controller.rb def about end #static_pages/about.html.erb <!DOCTYPE html> <html> <head> <title>About | Ruby on Rails Tutorial</title> </head> about </html> 其餘相似
測試:bundle exec rake test
3.REWORK
佈局和嵌入式Ruby
app/views/layouts/application.html.erb
<!DOCTYPE html> <html> <head> <title><%= yield(:title)%></title> <%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true %> <%= javascript_include_tag 'application', 'data-turbolinks-track' => true %> <%= csrf_meta_tags %> </head> <body> <%= yield %> </body> </html>
about.html.erb
<% provide(:title, "about"%> about #其餘相似
測試:bundle exec rake test
高級測試技術,MiniTest報告程序(下一遍添加)