尽管有正确的消息,但Rspec验证with_message测试失败

时间:2016-10-31 03:44:45

标签: ruby-on-rails ruby validation rspec

我在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}}

为什么这个测试无法通过的任何想法?

1 个答案:

答案 0 :(得分:4)

get_dummies值的空格看起来不一致。在message模型中,看起来User

之后有两个空格
are.

而在您的spec文件中,您只有一个空格

"People will want to know who you are.  Please include your name."

它们不是相同的字符串,因此测试失败