lundi 27 juillet 2020

Refactoring test with shoulda, the minitest way

I was browsing shoulda matchers and context in order to refactor some text and make them more coincise and readable, but thorough, the documentation I could not find how to test this in specific:

(I'm following Hartl tutorial, and I'm trying to refactor in the Minitest way, not 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' }

this:

user_test

user_model

attr_accessor :remember_token, :activation_token, :reset_token

before_save   :downcase_email
before_create :create_activation_digest
 test 'feed should have the right posts' do
    michael = users(:michael)
    archer  = users(:archer)
    lana    = users(:lana)
    # Posts from followed user
    lana.microposts.each do |post_following|
      assert michael.feed.include?(post_following)
    end
    # Self-posts for user with followers
    michael.microposts.each do |post_self|
      assert michael.feed.include?(post_self)
    end
    # Self-posts for user with no followers
    archer.microposts.each do |post_self|
      assert archer.feed.include?(post_self)
    end
    # Posts from unfollowed user
    archer.microposts.each do |post_unfollowed|
      assert_not michael.feed.include?(post_unfollowed)
    end
  end

this:

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]

Aucun commentaire:

Enregistrer un commentaire