Rspecでテストする方法を学ぼうとしています。
現時点では、Item クラスの仕様があります。
require 'spec_helper'
describe Item do
it { should belong_to :list }
before(:each) do
@item = FactoryGirl.create(:item)
end
subject { @item }
it { should respond_to :name }
it { should validate_presence_of :name }
end
ただし、これについていくつか質問があります。
it { should validate_presence_of :name }
書くのと同じです:
describe "when name is not present" do
before { @item.name = "" }
it { should_not be_valid }
end
または、両者の間に決定的な違いはありますか?
it { should belong_to :list }
また、仕様に書く価値があるかどうか、またはこれのためのより良い方法があるかどうかも疑問に思っていました.
@item = FactoryGirl.build(:item)
またはができることも知っていますFactoryGirl.create(:item)
。create は項目を test db に保存しますが、build は保存しませんか? それとも私はここで混乱していますか?いつどれを使用する必要がありますか?
ありがとう。