Rspec 是否应该匹配不匹配

Rspec 是否应该匹配不匹配,rspec,ruby-on-rails-6,shoulda-matchers,Rspec,Ruby On Rails 6,Shoulda Matchers,我正在创建一个文本消息模型来管理通信。 我在用'来测试 我的模型 class TextMessage < ApplicationRecord validates :message, presence: true end 我可以在控制台上运行FactoryBot.create(:text_message)来创建文本消息,而不会出现任何问题。但是当我运行测试时,我得到了这个错误 TextMessage is expected to validate that :message can

我正在创建一个文本消息模型来管理通信。 我在用'来测试

我的模型

class TextMessage < ApplicationRecord
  validates :message, presence: true
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

请出示你的短信工厂好吗?而错误的堆栈跟踪,其中是
self.message=message.gsub(“\u0000”,““”)
行?对我来说很有用™. 您可能有一些规范化属性的东西,试图去除空字节。@Schwern非常感谢您。我能够将
self.message=message.gsub(“\u0000”),更改为
self.message=message.to\u.gsub(“\u0000”),这使得测试通过将
nil
更改为空字符串,这可能很重要。为避免以意外方式更改数据,请使用
self.message=message&.gsub(“\u0000”,”)
。谢谢,我会做更改的
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