Shoulda-Matchers和自定义错误消息

Mag*_*rld 1 rspec ruby-on-rails shoulda activemodel

我正在尝试使用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"]
Run Code Online (Sandbox Code Playgroud)

是否有一个我在missa-matchers中缺少的设置允许测试通过而不用担心错误消息或这是模块的限制?

ole*_*ole 8

如果您不在with_message匹配器上使用该方法,则它使用默认消息.

要使测试工作,您应该覆盖匹配器的默认消息:

it { expect(subject).to validate_uniqueness_of(:name).with_message("has already been taken") }
Run Code Online (Sandbox Code Playgroud)