花了好几天,终于把测试体系非常粗的过了一遍,还把环境搭起来,用费九牛二虎之力并不为过,中间那些 小问题太多,尤其是 bundle install 的问题。
发乱文笔记一篇,仅供胡乱点击之用。
几个重要文件,guardfile, spec_helper.rb 都需要微调才行,不然每次 spork 都要跑很长时间,重复载入一些不必要的环境和 bundle gem.
现在跑一下第一个 case,只要两秒钟,并可以自动通知。
感觉 ROR 新手的话,要搭建测试环境都不容易啊,要学的东西真不少,看看测试用的 GEM.
group :development, :test do
gem 'capistrano', '2.9.0'
gem 'chunky_png', "1.2.5"
gem "memcache-client", "1.8.5"
gem 'progress_bar'
gem 'rspec-rails', '~> 2.8.1'
gem 'factory_girl_rails' , '~> 3.0.0'
gem 'thin'
gem "simplecov", :require => false
gem "rspec-cells"
gem "capybara"
gem "sunspot-rails-tester"
gem 'guard'
gem 'guard-rspec'
gem 'libnotify'
gem 'spork-rails'
gem 'guard-spork'
gem 'launchy'
end
---guard file
guard 'rspec',:cli => "--drb", :version => 2 do watch(%r{^spec/.+_spec.rb$}) watch(%r{^lib/(.+).rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" } watch('spec/spec_helper.rb') { "spec" }
# Rails example watch(%r{^app/(.+).rb$}) { |m| "spec/#{m[1]}spec.rb" } watch(%r{^app/(.*)(.erb|.haml)$}) { |m| "spec/#{m[1]}#{m[2]}_spec.rb" } watch(%r{^app/controllers/(.+)(controller).rb$}) { |m| ["spec/routing/#{m[1]}routing_spec.rb", "spec/#{m[2]}s/#{m[1]}#{m[2]}_spec.rb", "spec/acceptance/#{m[1]}_spec.rb"] } watch(%r{^spec/support/(.+).rb$}) { "spec" } watch('config/routes.rb') { "spec/routing" } watch('app/controllers/application_controller.rb') { "spec/controllers" } # Capybara request specs watch(%r{^app/views/(.+)/.*.(erb|haml)$}) { |m| "spec/requests/#{m[1]}_spec.rb" } end
guard 'spork', :cucumber_env => { 'RAILS_ENV' => 'test' }, :rspec_env => { 'RAILS_ENV' => 'test' } , :bundler => false do watch('config/application.rb') watch('config/environment.rb') watch(%r{^config/environments/.+.rb$}) watch(%r{^config/initializers/.+.rb$}) watch('Gemfile') watch('Gemfile.lock') watch('spec/spec_helper.rb') { :rspec } watch('test/test_helper.rb') { :test_unit } watch(%r{features/support/}) { :cucumber } end
---spec_helper.rb Spork.prefork do ENV["RAILS_ENV"] ||= 'test' require File.expand_path("../../config/environment", FILE) require 'rspec/rails' require 'rspec/autorun' require 'capybara/rspec'
Dir[Rails.root.join("spec/support/*/.rb")].each {|f| require f} end Spork.each_run do
end
RSpec.configure do |config| config.infer_base_class_for_anonymous_controllers = false end
--devise speed up test https://github.com/plataformatec/devise/wiki/Speed-up-your-unit-tests
--Rspec Doc https://www.relishapp.com http://rubydoc.info/gems/rspec-core
-Rspec 一些基本概念
--先来一个最最简单的例子 describe Thing do it "does something" do end end
--ExampleGroup and example describe 返回一个 ExampleGroup 对象,同时实例化一个 Behavior 对象 it 返回一个 Example 对象 Thing 是被测试的对象,后面通过调用 Thing 的一些方法,判断返回结果来决定测试是否成功 do end 之间是一个代码块,被 Rspec 框架调用进去
-- http://rubydoc.info/gems/rspec-core/RSpec/Core Rspec core 全部的名字空间 - 一一理解很重要 DSL, Formatters, Hooks, Let, MockFrameworkAdapter, Pending, RubyProject, SharedContext, SharedExampleGroup, Subject, Version Classes: CommandLine, Configuration, DRbCommandLine, Example, ExampleGroup, FilterManager, Metadata, Parser, ProjectInitializer, RakeTask, Reporter, Runner, World
DSL --Adds the describe method to the top-level namespace.
--Hooks http://rubydoc.info/gems/rspec-core/RSpec/Core/Hooks after/around/before [:each, :all, :suite]
---after Declare a block of code to be run after each example (using :each) or once after all examples (using :all).
--around
==Rspec expectations http://rubydoc.info/gems/rspec-expectations
--Rspec 的一些 Generator --FactoryGirl 以及 capabra 学习最基本的测试组件,Rspec 只是一个粘合 rails 和他们的框架 --rspec_rails 学习 先看 reademe https://github.com/rspec/rspec-rails
--factory_girl get started http://rdoc.info/github/thoughtbot/factory_girl/file/GETTING_STARTED.md
--capybara https://github.com/jnicklas/capybara
--guard http://rubydoc.info/github/guard/guard/master/frames http://railscasts.com/episodes/264-guard
--spork and guard http://blog.carbonfive.com/2010/12/10/speedy-test-iterations-for-rails-3-with-spork-and-guard/
bundle exec spork --bootstrap 这个命令载入 spec_helper 里面的环境 bundle exec spork
--growl for linux http://mattn.github.com/growl-for-linux/ https://github.com/mattn/growl-for-linux/downloads install libcurl4-openssl-dev
--change to libnotify gem, which works perfect no ruby-gntp gem
--Insecure world writable dir sudo chmod 775 -R ./rubychina
--单独测试 Ruser 失败,因为 user model 耦合了 Rforum,想分离用户的业务模快 --试着在 Rforum 里面做测试吧,考虑到模块的独立性,应该先在 Rforum 里面通过测试
bundle install --no-deployment bundle install --deployment rails generate --help to find rspec related generator rails generate rspec:install bundle exec spork --bootstrap, sport write something into spec_helper.rb bundle exec guard init spork guard init bundle exec guard start
--change maxim watch limit http://blog.sorah.jp/2012/01/24/inotify-limitation $ cat /proc/sys/fs/inotify/max_user_watches 8192 $ echo 100000|sudo tee /proc/sys/fs/inotify/max_user_watches Password: 100000 $ cat /proc/sys/fs/inotify/max_user_watches 100000
--rspec spork 笔记 http://rubyer.me/blog/1477
-- .rspec 要加上--drb --in guardfile guard 'rspec',:cli => "--drb", :version => 2 do...end -- add gem 'launchy' for capybara save_and_open_page