我是新來的鐵軌,並試圖寫我的第一個應用程序。我有一個列order_size表:整數和價格:十進制(8,5)。價格欄保存貨幣價格,因此需要非常精確,以防萬一您想知道。我正在試寫測試,以確保價格和order_size是一個正數,但無論我做什麼,他們都不會通過。簡單的Rspec測試正數不會通過
這裏是Rspec的測試
it "should require a positive order size" do
@attr[:order_size] = -23
@user.orders.create!(@attr).should_not be_valid
end
it "should require a positive price" do
@attr[:price] = -1.2908
@user.orders.create!(@attr).should_not be_valid
end
下面是Order類驗證
validates_presence_of :user_id
validates_numericality_of :order_size, :greater_than => 0,
:only_integer => true
validates_numericality_of :price, :greater_than => 0
這裏的測試結果
Failures:
1) Order validations should require a positive order size
Failure/Error: @user.orders.create!(@attr).should_not be_valid
ActiveRecord::RecordInvalid:
Validation failed: Order size must be greater than 0
# ./spec/models/order_spec.rb:39:in `block (3 levels) in <top (required)>'
2) Order validations should require a positive price
Failure/Error: @user.orders.create!(@attr).should_not be_valid
ActiveRecord::RecordInvalid:
Validation failed: Price must be greater than 0
# ./spec/models/order_spec.rb:44:in `block (3 levels) in <top (required)>'
究竟這是怎麼回事?我甚至試着運行測試,聲稱他們應該是有效的,但他們仍然失敗。任何幫助,將不勝感激。
事實上,我也會像這樣測試它:純粹依靠'訂單'。訂單的驗證並不取決於它是否屬於'@ user'的一部分。 – nathanvda 2011-04-29 00:15:27