RSpec2.4.0ãRails3.0.3ã§å¦ã¶(ãã®1)
åæ©
Ruby on Railsã¨RSpecã®å
¥éã«ããã£ã¦るびまãåèã«ããã¦ããã£ããä¸å¯§ã«èª¬æããã¦ãã¦ãã¨ã¦ãããè¨äºã ã¨æã£ãã
ãã ãRailsã¨RSpecãã¼ã¸ã§ã³ãä¸ãã£ã¦ã³ãã³ããªã©ã«éããåºã¦ãã¾ã£ã¦ããããããã®ã¾ã¾ã§ã¯å®è·µã§ããªãã
ããããæ¸ããã¨ã¯ãRails=3.0.3ã¨RSpec=2.4.0ãå©ç¨ãã¦ãä¸è¨ãµã¤ãããRSpecãå¦ã¶ã®ããæä¼ãã§ããã°ãããªã¨æããå·®ç°ã®ã¿ãªã®ã§ãå
ã®è¨äºãèªã¿ãªãããã³ãã³ãå®è¡æãã³ã¼ãè¨è¿°æãªã©ã«ãã¡ãã®å
容ãè¦ã¦ã»ããã
å 容
Railsã®ãã¼ã¸ã§ã³ç¢ºèª
rails -v
Rails ããã¸ã§ã¯ãã®ä½æ
rails new myblog -T -J
Gemfileã®ä¿®æ£
Gemfile
group :test, :development do gem "rspec-rails", "~> 2.4" end
ã¸ã§ãã¬ã¼ã¿ã§ Blog ã¢ãã«ãçæãã
rails generate scaffold Blog name:string
ãã¤ã°ã¬ã¼ã·ã§ã³ãã¡ã¤ã«ãç·¨éãã
db/migrate/yyyyMMddhhmmss_create_blogs.rb
class CreateBlogs < ActiveRecord::Migration def self.up create_table :blogs do |t| t.string :name, :null => false # null ä¸å¯ã«ãã t.timestamps end end def self.down drop_table :blogs end end
ãã¼ã¿ãã¼ã¹ãä½æãã
rake db:migrate
magic comment
Ruby1.9ã®å ´åãæ¥æ¬èªããã¡ã¤ã«å ã§ä½¿ç¨ããã¨ä¾å¤ãçºçããããããåé¿ããã«ã¯ãä¸è¨ã®ã³ã¼ãããã¡ã¤ã«ã®æåã®è¡ã«å ¥ããå¿ è¦ãããããã以éã®specãã¡ã¤ã«ã®æåã®è¡ã«ã¯å¿ ãå ¥ãããã¨ãçºçããçç±ã«ã¤ãã¦ã¯ãhttp://jp.rubyist.net/magazine/?0025-Ruby19_m17n#l18ã詳ããã
# coding: utf-8
Blog ã®ããªãã¼ã·ã§ã³ã®ã¹ããã¯ãå®ç¾©ãã
spec/models/blog_spec.rb
describe Blog, "#name ãè¨å®ããã¦ããªãå ´å" do before(:each) do @blog = Blog.new @blog.should have(1).errors_on(:name) end it "ããªãã¼ã·ã§ã³ã«å¤±æãããã¨" do @blog.should_not be_valid end end
Blog ã¯ã©ã¹ã«æå¾ ãããæ¯èãå®è£ ãã
app/models/blog.rb
class Blog < ActiveRecord::Base validates :name, :presence => true end
app/model/blog_spec.rb
describe Blog, "#name ãè¨å®ããã¦ããªãå ´å" do before(:each) do @blog = Blog.new @blog.should have(1).errors_on(:name) end it "ããªãã¼ã·ã§ã³ã«å¤±æãããã¨" do @blog.should_not be_valid end end
ã¸ã§ãã¬ã¼ã¿ã§ Entry ã¢ãã«ãçæãã
rails generate model Entry title:string body:text posted_on:date created_at:timestamp updated_at:timestamp blog_id:integer
ãã¤ã°ã¬ã¼ã·ã§ã³ãã¡ã¤ã«ãç·¨éãã
db/migrate/yyyyMMddhhmmss_create_entries.rb
class CreateEntries < ActiveRecord::Migration def self.up create_table :entries do |t| t.string :title, :null => false t.text :body, :null => false t.date :posted_on t.timestamp :created_at t.timestamp :updated_at t.integer :blog_id, :null => false t.timestamps end end def self.down drop_table :entries end end
(spec/fixturesãä½æãã)
ãªããspec/fixturesãä½æãããªãã£ããããèªåã§ä½æãããymlãã¡ã¤ã«ã¯ãã®ä¸ã«ç½®ãã°ããã
mkdir spec/fixtures
ãEntry ã¯ç¹å®ã® Blog ã«å±ãããã¨ããã¹ããã¯ã¨ãã¦å®ç¾©ãã
spec/models/entry_spec.rb
describe Entry do fixtures :entries, :blogs before(:each) do @entry = entries(:kakutani_earliest) end it "ã¯ç¹å®ã®ããã°ã«å±ãããã¨" do @entry.blog.should == blogs(:kakutani) end end
Entry ã« belongs_to ãå®è£ ãã
app/models/entry.rb
class Entry < ActiveRecord::Base belongs_to :blog end
Blog ã« has_many ãå®è£ ãã
app/models/blog.rb
class Blog < ActiveRecord::Base validates :name, :presence => true has_many :entries end
è¨äºã®æ稿æ¥ã«æå¾ ããæ¯èãå®ç¾©ãã
app/models/entry.rb
# å ã®è¨äºã®ã¾ã¾ãbefore_saveããå©ç¨ããã¨ãDEPRECATION WARNINGãçºçãã # ä¸è¨ã®ããã«ãã¡ã½ãããä½æãã¦å®ç¾©ããã¨è§£æ±ºãã class Entry < ActiveRecord::Base belongs_to :blog before_save :fill_posted_on def fill_posted_on self.posted_on ||= Date.today end end