【问题标题】:How to use shoulda-matchers with Minitest Specs style in Rails?如何在 Rails 中使用具有 Minitest Specs 样式的 shoulda-matchers?
【发布时间】:2016-02-19 23:22:58
【问题描述】:

Minitest 用法的shoulda-matchers 文档提供了如何在断言样式中使用它们的示例,如下所示:

class PersonTest < ActiveSupport::TestCase
  should validate_presence_of(:name)
end

但是如何使用 Minitest 的 Spec 样式来使用它们?

以下产生错误:Minitest::UnexpectedError: NoMethodError: undefined method 'validate_presence_of'

describe Person do
  it { should validate_presence_of(:name) }
end

【问题讨论】:

标签: ruby-on-rails minitest shoulda


【解决方案1】:

它实际上与原始方式非常相似。对于我的一个个人项目,这就是我的VideoTest 课程的样子,并且我一直在使用 MiniTest Spec。

require 'test_helper'

describe VideoTest do
  let(:video) { build :video }

  should belong_to :course

  should validate_presence_of :title
  should validate_presence_of :description
end

【讨论】:

  • 谢谢本。这是对的。我很确定我最初是这样做的并且它不起作用,但是我再次尝试它并且它起作用了。您的回答有助于验证它应该有效。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多