Shoulda-Matchers和自定义错误消息

时间:2016-10-14 22:40:12

标签: ruby-on-rails rspec activemodel shoulda

我正在尝试使用shoulda匹配器进行一些基本的rspec测试,并且我发现了一个错误,我之前没有看到过这样的问题。

我有一个名为name的唯一属性,但由于项目所需的原因,我已经覆盖了默认的"已经被采取了"在config / locales / en.yml中使用我自己的消息形式的消息,而且似乎不喜欢它。

我收到了此错误消息

 Failure/Error: it { should validate_uniqueness_of(:name) }

   Flavor did not properly validate that :name is case-sensitively unique.
     Given an existing Flavor whose :name is ‹"Factory Flavor Namea"›,
     after making a new Flavor and setting its :name to ‹"Factory Flavor
     Namea"› as well, the matcher expected the new Flavor to be invalid and
     to produce the validation error "has already been taken" on :name. The
     record was indeed invalid, but it produced these validation errors
     instead:

     * name: ["This flavor name is already in the system"]
     * abbreviation: ["This abbreviation is already in use"]

在shoulda-matchers中是否缺少允许测试通过而不担心错误消息的设置,或者这是模块的限制吗?

1 个答案:

答案 0 :(得分:7)

如果您未在匹配器上使用with_message方法,则uses default message

要使您的测试有效,您应该覆盖matcher's default message

it { expect(subject).to validate_uniqueness_of(:name).with_message("has already been taken") }