Rails3 㧠RSpec + RCov æ¥æ¬èªå¯¾å¿
Rails3 ã§ããã¸ã§ã¯ããä½æããRSpec 㨠RCov ãæ¥æ¬èªã§åä½ãããã¾ã§ã®æé ã§ãã
rails (3.0.0) ã rspec (2.0.0.beta.22) çã®å¿
è¦ãª gem ã¯æ¢ã«ã¤ã³ã¹ãã¼ã«ããã¦ãããã®ã¨ãã¾ãã
ã¾ãã¯ãæ®éã« Rails ããã¸ã§ã¯ããä½æãã¦ãconfig/database.yml ãéçºç°å¢ã«åããã¦è¨å®ãã¦ããã¾ãã
$ rails new project create create README create Rakefile create config.ru create .gitignore create Gemfile ãããããï¼ ãããããï¼
ããã¸ã§ã¯ãå ã«ã¦ RSpec 㨠RCov ã使ç¨ããããã« Gemfile ã«ä¸è¨ã追è¨ãã¾ãã
gem "rcov" group :test do gem 'rspec-rails', '>= 2.0.0.beta', :group => :development end
ããã¸ã§ã¯ãã®ãã¹ããã¬ã¼ã ã¯ã¼ã¯ã RSpec ã«ããããã«
config/application.rb ã«ä¸è¨ã®ãããªä¿®æ£ãå ãã¾ãã
module Project class Application < Rails::Application config.generators do |g| g.test_framework :rspec end end end
RSpec ãã¹ãç¨ã®è¨å®ãã¡ã¤ã«ãè¨ç½®ãã¾ãã
$ ./script/rails generate rspec:install create .rspec create spec create spec/spec_helper.rb create autotest create autotest/discover.rb
ããã¾ã§åºæ¬çãªæºåã¯å®äºã§ãã
controller ãªã©ãä½æããã¨ãåæã« rspec ç¨ã®ãã¹ããã¡ã¤ã«ãçæããã¾ãã
$ ./script/rails generate controller user index
create app/controllers/user_controller.rb
route get "user/index"
invoke erb
create app/views/user
create app/views/user/index.html.erb
invoke rspec
invoke helper
create app/helpers/user_helper.rb
invoke rspec
create spec/helpers/user_helper_spec.rb
ãã㧠RSpec ãå®è¡ããç°å¢ã¯ã§ãã¾ããã®ã§ãä¸è¨ã®ããã«ãã¹ããæ¸ãã¦ã¿ã¾ãã
1 è¡ç®ã® "# coding: utf-8" ãè¨è¿°ãã¦ãããã¨ã§ããã¹ãå
容ãæ¥æ¬èª UTF-8 ã§è¨è¿°ãã¦å®è¡ãããã¨ãå¯è½ã«ãªãã¾ãã
# coding: utf-8 require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') describe UserController do describe 'index method' do it 'GET "index" ãªã¯ã¨ã¹ããæåãããã¨' do get 'index' response.should be_success end end end
次㫠RSpec ã§è¨è¿°ãããã¹ãã®ã«ãã¬ãã¸ã確èªããããã« RCov ãå®è¡ãããã®ã§ããã
æ®éã« rake spec:rcov ãå®è¡ããã¨ãä¸è¨ã®ãããªã¨ã©ã¼ãåºã¦å®è¡ã§ãã¾ããã
ããã¯ãRSpec ãã¹ãå
ã®æ¥æ¬èª UTF-8 ãåé¡ã§æåãæ£å¸¸ã«èªèã§ãã¦ããªãããã«çºçããåé¡ã®ããã§ãã
$ rake spec:rcov (in /Users/nedate/svn/project) /usr/local/bin/ruby -S bundle exec rcov --exclude /gems/,/Library/,/usr/,lib/tasks,.bundle,config,/lib/rspec/,/lib/rspec- "./spec/controller/user_spec.rb" ** WARNING: Ruby 1.9 Support is experimental at best. Don't expect correct results! ** /usr/local/lib/ruby/gems/1.9.1/gems/rcov-0.9.9/lib/rcov/file_statistics.rb:115:in `block in is_code?': invalid byte sequence in US-ASCII (ArgumentError) from /usr/local/lib/ruby/gems/1.9.1/gems/rcov-0.9.9/lib/rcov/file_statistics.rb:112:in `each' from /usr/local/lib/ruby/gems/1.9.1/gems/rcov-0.9.9/lib/rcov/file_statistics.rb:112:in `each_with_index' from /usr/local/lib/ruby/gems/1.9.1/gems/rcov-0.9.9/lib/rcov/file_statistics.rb:112:in `is_code?' from /usr/local/lib/ruby/gems/1.9.1/gems/rcov-0.9.9/lib/rcov/file_statistics.rb:234:in `extend_heredocs' from /usr/local/lib/ruby/gems/1.9.1/gems/rcov-0.9.9/lib/rcov/file_statistics.rb:40:in `initialize' from /usr/local/lib/ruby/gems/1.9.1/gems/rcov-0.9.9/lib/rcov/formatters/base_formatter.rb:50:in `new' from /usr/local/lib/ruby/gems/1.9.1/gems/rcov-0.9.9/lib/rcov/formatters/base_formatter.rb:50:in `add_file' from /usr/local/lib/ruby/gems/1.9.1/gems/rcov-0.9.9/lib/rcov/code_coverage_analyzer.rb:143:in `block (2 levels) in dump_coverage_info' from /usr/local/lib/ruby/gems/1.9.1/gems/rcov-0.9.9/lib/rcov/code_coverage_analyzer.rb:142:in `each' from /usr/local/lib/ruby/gems/1.9.1/gems/rcov-0.9.9/lib/rcov/code_coverage_analyzer.rb:142:in `block in dump_coverage_info' from /usr/local/lib/ruby/gems/1.9.1/gems/rcov-0.9.9/lib/rcov/code_coverage_analyzer.rb:135:in `each' from /usr/local/lib/ruby/gems/1.9.1/gems/rcov-0.9.9/lib/rcov/code_coverage_analyzer.rb:135:in `dump_coverage_info' from /usr/local/lib/ruby/gems/1.9.1/gems/rcov-0.9.9/bin/rcov:433:in `block in <top (required)>' rake aborted! Command failed with status (1): [/usr/local/bin/ruby -S bundle exec rcov --...] (See full trace by running task with --trace)
ããã¯ãspec/spec_helper.rb ã«ä¸è¨ã®ããã«è¨è¿°ãããã¨ã§åé¿ã§ãã¾ãã
# # quick monkey patch for rcov # # http://codefluency.com/post/1023734493/a-bandaid-for-rcov-on-ruby-1-9 # if defined?(Rcov) class Rcov::CodeCoverageAnalyzer def update_script_lines__ if '1.9'.respond_to?(:force_encoding) SCRIPT_LINES__.each do |k,v| v.each { |src| src.force_encoding('utf-8') } end end @script_lines__ = @script_lines__.merge(SCRIPT_LINES__) end end end