简体   繁体   中英

Rails Minitest model method test

I've got User model method which calculate a number of user membership. I want to it test by MiniTest. Here is what I have:

  def member_for
    time_diff = Time.current - created_at
    result = ActiveSupport::Duration.build(time_diff.to_i).parts
    return "Member for #{result[:years]}y, #{result[:months]}m" if result.key?(:years) && result.key?(:months)

    if result.key?(:years) && result.key?(:days)
      "Member for #{result[:years]}y, #{result[:days]}d"
    elsif result.key?(:months)
      "Member for #{result[:months]}m"
    elsif result.key?(:days)
      "Member for #{result[:days]}d"
    end
  end

I was trying to write some MiniTest:

  test 'member for' do
    user.created_at = 2.years.ago + 3.months + 2.days
    user.member_for
  end

  private

  def user
    @user ||= users(:one)
  end

But to be honest, I don't know how to compare if it returns the correct string.

You can decrease the cyclic complexity as well as removing several bugs which give the wrong duration (by omitting weeks and months) by simply iterating across the parts:

  def member_for
    time_diff = Time.current - created_at
    parts = ActiveSupport::Duration.build(time_diff.to_i).parts
    formatted = parts.except(:seconds).map do |unit, value|
      "#{value} #{unit}"
    end
    "Member for #{formatted.to_sentance}"     
  end

You can use a simple lookup table or the I18n API if you want to have abbreviations instead of the full unit names. Array#to_sentance is from ActiveSupport.

You would then test this by:

class MemberTest < ActiveSupport::TestCase
  test 'member_for without weeks' do
    user.created_at = 2.years.ago + 3.months + 2.days
    assert_equal(user.member_for, 'Member for 1 year, 3 months and 2 days')
  end

  test 'member_for with weeks' do
    user.created_at = 2.years.ago + 2.weeks
    assert_equal(user.member_for, 'Member for 1 year and 2 weeks')
  end

  private

  def user
    @user ||= users(:one)
  end
end

However its questionable if this code really belongs in a model in the first place since its presentation and not buisness logic. I would say a helper or decorator/presenter is more suitible then cramming more logic into your models.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM