首页 > 解决方案 > shoulda-matchers 不匹配

问题描述

我正在创建一个 TextMessage 模型来管理通信。我正在使用shoulda-matchers进行测试

我的模型

class TextMessage < ApplicationRecord
  validates :message, presence: true
end

现在我的测试

RSpec.describe TextMessage, type: :model do
  subject { FactoryBot.create(:text_message) }
  it { is_expected.to validate_presence_of :message }
end

我可以在控制台上运行 FactoryBot.create(:text_message) 来创建一条没有任何问题的文本消息。但是当我运行我的测试时,我得到了这个错误

TextMessage
  is expected to validate that :message cannot be empty/falsy (FAILED - 1)

Failures:

  1) TextMessage is expected to validate that :message cannot be empty/falsy
     Failure/Error: self.message = message.gsub("\u0000", '')

     NoMethodError:
       undefined method `gsub' for nil:NilClass

标签: rspecruby-on-rails-6shoulda-matchers

解决方案


推荐阅读