用shoulda重构测试,minitest方式

Refactoring test with shoulda, the minitest way

我正在浏览 shoulda 匹配器和上下文,以便重构一些文本并使它们更加一致和可读,但彻底,我找不到如何具体测试它的文档:

(我正在学习 Hartl 教程,我正在尝试以 Minitest 方式进行重构,而不是 Rspec

micropost_model

default_scope -> { order(created_at: :desc) }

has_one_attached :image

&

validates :image,   content_type: { in: %w[image/jpeg image/gif image/png],
                                      message: 'must be a valid image format' },
                    size: { less_than: 5.megabytes,
                            strong textmessage: 'should be less than 5MB' }

这个:

user_test

user_model

attr_accessor :remember_token, :activation_token, :reset_token

before_save   :downcase_email
before_create :create_activation_digest

这个:

routes

  resources :account_activations, only: [:edit]
  resources :password_resets,     only: %i[new create edit update]
  resources :microposts,          only: %i[create destroy]
  resources :relationships,       only: %i[create destroy]

一般来说,您可以通过以下方式测试您发布的作品:

  • 应该匹配器 soon 有一个 have_one_attached 匹配器可以用来测试 has_one_attached.
  • 您为 Micropost 发布的验证来自 Paperclip 和 Paperclip provides matchers you can use in your tests
  • 您可以使用 Shoulda 中的验证匹配器代替 user_test 中的前几个测试——例如,should validate_presence_of(:email)should validate_length_of(:email).is_at_most(255)
  • Shoulda 不提供任何工具来测试与身份验证或回调相关的代码 — 您将不得不查看您的用户模型在做什么,并围绕逻辑手动编写测试。从您发布的代码片段来看,这可能包括创建模型实例、设置一些属性、保存模型以及断言某些属性已设置。
  • 有一种方法可以使用 route 使用 Shoulda 测试路由。关于这一点,我要说的是,在编写集成或系统测试时,您可能会在每次测试中获得更好的里程数,这些测试会执行可通过路由访问的功能,而不是直接测试路由。但至少如果你想要那个级别的粒度,那个匹配器就在那里。

希望对您有所帮助 — 如果您需要更多帮助,请告诉我。