我在Rspec中进行了一项测试,旨在验证特定的验证消息。当我运行测试服时,它失败了。但是根据失败描述的外观,它看起来应该是通过。这是测试:
User did not properly validate that :name cannot be empty/falsy,
producing a custom validation error on failure.
After setting :name to ‹nil›, the matcher expected the UserProfile to
be invalid and to produce a validation error matching ‹/People will
want to know who you are. Please include your name./› on :name. The
record was indeed invalid, but it produced these validation errors
instead:
* email: ["can't be blank", "Please enter valid email address."]
* name: ["People will want to know who you are. Please include your
name."]
* password: ["can't be blank"]
这是我运行测试套件时收到的失败消息:
故障/错误: is_expected.to validate_presence_of(:name)。 with_message( /人们想知道你是谁。请包括你的名字./ )
User.rb
以下是在模型 validates_presence_of :name,
message: "People will want to know who you are. Please include your name."
{{1}}
为什么这个测试无法通过的任何想法?
答案 0 :(得分:4)
get_dummies
值的空格看起来不一致。在message
模型中,看起来User
are.
而在您的spec文件中,您只有一个空格
"People will want to know who you are. Please include your name."
它们不是相同的字符串,因此测试失败