【发布时间】:2012-01-24 19:45:09
【问题描述】:
我正在尝试整个 TDD,但遇到了验证存在的问题。我有一个名为 Event 的模型,我想确保在创建 Event 时存在 title、price 和 summary。
单元测试代码
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 失败。哪个好。
现在我想在Event 模型中使用以下代码首先让title 测试通过。
class Event < ActiveRecord::Base
validates :title, :presence => true
end
当我重新运行测试时,我得到 3 次通过,而我认为我应该得到 1 次通过和 2 次失败。为什么我会获得 3 次通过?
【问题讨论】:
标签: ruby-on-rails ruby unit-testing validation testing