我試圖學習如何測試與Rspec。測試存在和belongs_to與Rspec
目前,我有一個項目類規格:
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)
。是否創建將項目保存到測試數據庫並且不生成?或者我在這裏困惑。我應該什麼時候使用哪個?
謝謝。
謝謝。我的確在使用shoulda-matchers。我會用短表格! :) – cabaret 2012-08-12 17:56:14