Created
May 31, 2010 02:35
-
-
Save itabatakafumi/419484 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#http://jp.rubyist.net/magazine/?0021-Rspec#l28 | |
#Arrayクラスのテストコード(配列が空の場合) | |
array_spec.rb | |
describe Array, "when empty" do | |
before do | |
@empty_array = [] | |
@array = Array.new(3){ Hash.new } | |
@array[0][:cat] = "Nuko" | |
end | |
it "should be empty" do | |
@empty_array.should be_empty | |
end | |
it "should size 0" do | |
@empty_array.size.should == 0 | |
end | |
it "should not affect others" do | |
@array.should == [{:cat => "Nuko"}, {}, {}] | |
end | |
after do | |
@empty_array = nil | |
end | |
end | |
#http://jp.rubyist.net/magazine/?0021-Rspec#l50 で実装したテストコード | |
#modelのテストコード | |
/spec/models/blog_spec.rb | |
require 'spec_helper' | |
describe Blog, "#name が設定されていない場合:" do | |
before(:each) do | |
@blog = Blog.new | |
end | |
it "バリデーションに失敗すること" do | |
@blog.should_not be_valid | |
end | |
it ":name にエラーが設定されていること" do | |
@blog.should have(1).errors_on(:name) | |
end | |
end | |
describe Blog, "Entryを所有する" do | |
fixtures :blogs, :entries | |
before do | |
@blog = blogs(:kakutani) | |
end | |
it "は複数の記事を所有できること" do | |
@blog.should have_at_least(2).entries | |
end | |
end | |
describe Blog, "に記事を投稿できた場合" do | |
fixtures :blogs, :entries | |
before do | |
@blog = blogs(:kakutani) | |
end | |
it "記事の件数が1件増えること" do | |
lambda { | |
@blog.entries.create( | |
:title => 'new_post', :body => 'hello', | |
:posted_on => Date.today) | |
}.should change(Entry, :count).by(1) | |
end | |
end | |
/spec/models/entry_spec.rb | |
require 'spec_helper' | |
describe Entry do | |
fixtures :entries, :blogs | |
before(:each) do | |
@entry = entries(:kakutani_earliest) | |
end | |
it "は特定のブログに属すること" do | |
@entry.blog.should == blogs(:kakutani) | |
end | |
end | |
describe Entry, "#posted_on が入力されずに保存された場合:" do | |
fixtures :blogs | |
before do | |
@entry = Entry.new(:blog => blogs(:kakutani), | |
:title => "タイトル", :body => "本文") | |
@entry.save! | |
@entry.reload | |
end | |
it "Entry の作成日は投稿日であること" do | |
@entry.posted_on.should == Date.today | |
end | |
end | |
describe Entry, "#posted_on を明示して保存された場合:" do | |
fixtures :blogs | |
before do | |
@posted_on = Date.today - 10 | |
@entry = Entry.new(:blog => blogs(:kakutani), | |
:title => "タイトル", :body => "本文", | |
:posted_on => @posted_on) | |
@entry.save! | |
@entry.reload | |
end | |
it "入力された日付が投稿日であること" do | |
@entry.posted_on.should == @posted_on | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment