我正在嘗試整個TDD,並且遇到了驗證存在的問題。我有一個名爲Event
的模型,我想確保當創建title
a price
和summary
時存在Event
。單元測試驗證是否存在奇怪的行爲
單元測試代碼
class EventTest < ActiveSupport::TestCase
test "should not save without a Title" do
event = Event.new
event.title = nil
assert !event.save, "Save the Event without title"
end
test "should not save without a Price" do
event = Event.new
event.price = nil
assert !event.save, "Saved the Event without a Price"
end
test "should not save without a Summary" do
event = Event.new
event.summary = nil
assert !event.save, "Saved the Event without a Summary"
end
end
運行測試,我得到3失敗。哪個好。 現在我想要title
測試通過Event
模型中的以下代碼首先通過。
class Event < ActiveRecord::Base
validates :title, :presence => true
end
當我重新運行測試,我得到3次,我會覺得我應該得到1 PASS和2失敗。爲什麼我得到3個通行證?
這工作得很好。感謝幫手。 – alenm