我正在嘗試一個挑戰,我需要做一些驗證。我應該通過以下測試:我的驗證有什麼問題?
it 'should not allow political words' do
body = "Petralha Tucanos Dilma"
opinion = Opinion.new(body: body)
expect(opinion).to_not be_valid
expect(opinion.errors[:body]).to include "Contains political words"
end
it 'is invalid when has no body' do
body = ''
opinion = Opinion.new(body: body)
expect(opinion).to_not be_valid
expect(opinion.errors[:body]).to include 'can\'t be blank'
end
it 'is invalid when has more than 150 caracters' do
body = 'a' * 151
opinion = Opinion.new(body: body)
expect(opinion).to_not be_valid
expect(opinion.errors[:body]).to include 'is too long'
end
要做到這一點我想在我的模型如下:
class Opinion < ActiveRecord::Base
validates :body, presence: true, length: { maximum: 150 }, exclusion: { in: %w(Petralha Tucanos Dilma) }
end
我還在一個on Rails的小白,所以任何有線索? 這些都是錯誤的,我得到:
1)意見不應該讓政治話語 故障/錯誤:期待(意見).to_not be_valid 預期#爲無效 #./spec/models/opinion_spec.rb :16:in'block(2 levels)in'
2)當超過150個字符時,意見無效 失敗/錯誤:期望(opinion.errors [:body])包含'太長' 預計[「太長(最多150個字符)」]包含「太長」 #./spec/models/opinion_spec.rb:33:in'block(2 levels)in'
在0.12861秒成品(文件了0.64205秒加載) 11例,2次失敗
失敗的例子:
rspec的./spec/models/opinion_spec.rb:12#輿論不應該讓政治words rspec ./spec/models/opinion_spec.rb:28#當超過150個字符時,意見無效
我編輯了您的問題以格式化代碼,以便它能夠正確顯示並使用語法高亮顯示。將來使用代碼按鈕「{}」或在每行新代碼的開頭添加4個空格(超出任何縮進範圍)。 – Beartech