测试 guard+rspec+spork 终于跑起来了

hunter · 2012年04月06日 · 最后由 hunter 回复于 2012年04月07日 · 5838 次阅读

花了好几天,终于把测试体系非常粗的过了一遍,还把环境搭起来,用费九牛二虎之力并不为过,中间那些 小问题太多,尤其是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

共收到 6 条回复

知识贴mark

@hunter 谢谢分享,不过感觉排版有点混乱,一些奇怪的格式充斥在其中,是否介意重新用 markdown 调整一下呢?这样方便他人阅读。

还有一个guard的好基友:livereload 放一起测试很方便,特别有两个屏,一个改,另一个改即显示

昨天也折腾了一下,但是还有点小问题:


Legend: ( ) - not detected in project   (*) - detected
Preloading Rails environment
Loading Spork.prefork block...
Spork is ready and listening on 8989!
ERROR: Could not reload Spork server for RSpec, Test::Unit after 30 seconds. I will continue waiting for a further 60 seconds.
ERROR: Could not reload Spork server for RSpec, Test::Unit. Make sure you can use it manually first.
Running: spec/requests/static_pages_spec.rb

实际上已经可以用了,但是会报这个错误,好像是原生的Test::Unit问题吧,不知到怎么去掉

原生的unittest,直接把test目录删除就可以了

liveload看了下,还没有使用,确实改变了开发的习惯,每次自己去刷新,不agie

需要 登录 后方可回复, 如果你还没有账号请点击这里 注册