You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Ruby Clock类非军事时间(12小时制)实现问题求助(附TDD测试)

Fixing Your 12-Hour Clock Conversion in Minitest TDD

Let's get your test_clocks_are_not_military_time test passing! The core issue right now is twofold: your not_military_time method has incorrect conversion logic, and your test isn't actually calling this method to get the 12-hour formatted time.

Your Current Test Code (TDD)

gem 'minitest', '~> 5.2'
require 'minitest/autorun'
require 'minitest/pride'
require_relative 'clock'

class ClockTest < Minitest::Test
  def test_start_at_6
    clock = Clock.new
    assert_equal 6, clock.time
  end

  def test_passage_of_time
    clock = Clock.new
    clock.wait
    assert_equal 7, clock.time
    3.times { clock.wait }
    assert_equal 10, clock.time
  end

  def test_clocks_are_not_military_time
    clock = Clock.new
    8.times { clock.wait }
    assert_equal 2, clock.time
  end
end

Your Current Clock Class Implementation

class Clock
  attr_reader :time

  def initialize
    @time = 6
  end

  def wait(time = 1)
    @time += time
  end

  def not_military_time
    @time += (time - 10)
  end
end

What's Going Wrong

  1. Unused Method: Your test expects clock.time to return 2, but time is still tracking 24-hour time (6 + 8 = 14). You wrote not_military_time but never called it in the test.
  2. Incorrect Conversion Logic: The not_military_time method's math doesn't follow 12-hour rules—adding time - 10 would turn 14 into 18, which is worse! We need a proper modulo-based conversion.

Solution 1: Make time Return 12-Hour Format

If your intent is for the time attribute to always show 12-hour formatted time, adjust the class to track 24-hour time internally and convert it on access:

class Clock
  def initialize
    @hours_24 = 6 # Track time in 24-hour format internally
  end

  def wait(amount = 1)
    @hours_24 += amount
  end

  def time
    # Convert 24-hour time to 12-hour
    converted = @hours_24 % 12
    converted.zero? ? 12 : converted # Handle 12:00 case (12 instead of 0)
  end

  # Optional: Keep this method if you want an explicit 12-hour method
  def not_military_time
    time
  end
end

This will make all your tests pass:

  • test_start_at_6: Returns 6 (correct 12-hour time)
  • test_passage_of_time: Returns 7, then 10 (both valid 12-hour times)
  • test_clocks_are_not_military_time: 6 + 8 = 14 → 14 % 12 = 2 (matches your expected result)

Solution 2: Use not_military_time in the Test

If you want time to stay as 24-hour format and have a separate method for 12-hour conversion, fix the method and update your test:

Updated Clock Class

class Clock
  attr_reader :time

  def initialize
    @time = 6
  end

  def wait(amount = 1)
    @time += amount
  end

  def not_military_time
    converted = @time % 12
    converted.zero? ? 12 : converted
  end
end

Updated Test

Modify the failing test to call not_military_time instead of time:

def test_clocks_are_not_military_time
  clock = Clock.new
  8.times { clock.wait }
  assert_equal 2, clock.not_military_time
end

Either approach will get your test passing. The first option makes the time attribute match your expected 12-hour behavior across all tests, which aligns with your current test assertions.

内容的提问来源于stack exchange,提问作者cjl85

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 07:30:38