8

我将以下代码放入 RSpec 测试中:

it { should validate_format_of(:email).not_with('test@test')}

并设置实际类:

validates :email, :presence => true, :format => /\b[A-Z0-9._%-]+@(?:[A-Z0-9-]+\.)+[A-Z]{2,4}\b/i

当我运行测试时,我得到:

失败:1)用户失败/错误:它 { should validate_format_of(:email).not_with('test@test')} 当电子邮件设置为“test@test”时,预期的错误包括“不能为空”,得到错误:[“名称不能为空(nil)”,“电子邮件无效(\“test@test\”)”]#./spec/models/user_spec.rb:8:在`block(2级)在 '

当我做一个通过测试时:

it { should validate_format_of(:email).with('adam@trimediatlantic.com')}

一切都按预期工作。有人可以告诉我我做错了什么还是这是一个框架问题。谢谢你。

4

2 回答 2

26

试试这个:

it { should_not allow_value("test@test").for(:email) }
于 2010-11-09T22:26:11.953 回答
3

I just ran into a similar problem Turns out you need to invoke the with_message method and supply the exact error message as a string, or a regex that matches the error message. Doing so will convince the validate_format_of to cease its stubborn insistence that format errors result in "can't be blank" messages, and actually pass. For example:

it { should validate_format_of(:email).not_with('test@test')}

becomes

it { should validate_format_of(:email).not_with('test@test').with_message(/invalid/)}

This sure looks like a bug in the shoulda library.

于 2011-03-15T18:54:13.297 回答