0

I just touch the unit test, today encountered a very strange question, I use assertThat to determine whether the two objects are equal, there is a test case can not pass, when I test the object:

sealed class SummaryViewState : MviViewState {

      /**
       * 默认显示曲线图和标签汇总状态(首次进入页面)
       */
      data class SummaryDataViewState(
          val points: List<Pair<Int, Float>>, // 曲线图点
          val months: List<Pair<String, Date>>, // 曲线图月份
          val values: List<String>, // 曲线图数值文本
          val selectedIndex: Int, // 曲线图选中月份索引
          val summaryItemList: List<SummaryListItem> // 当月标签汇总列表
      ) : SummaryViewState()

      /**
       * 切换月份时标签汇总状态
       */
      data class SummaryGroupingTagViewState(
          val summaryItemList: List<SummaryListItem> // 当月标签汇总列表
      ) : SummaryViewState()
  }

but look at the results are the same:

java.lang.AssertionError: 
Expected: is <[SummaryDataViewState(points=[(0, 100.0), (5, 200.0)], months=[(五月, Mon May 01 22:47:00 CST 2017), (六月, Thu Jun 01 22:47:00 CST 2017), (七月, Sat Jul 01 22:47:00 CST 2017), (八月, Tue Aug 01 22:47:00 CST 2017), (九月, Fri Sep 01 22:47:00 CST 2017), (十月, Sun Oct 01 22:47:00 CST 2017)], values=[¥100.00, ¥200.00], selectedIndex=5, summaryItemList=[SummaryListItem(tagName=晚餐, total=¥200.00)])]>
     but: was <[SummaryDataViewState(points=[(0, 100.0), (5, 200.0)], months=[(五月, Mon May 01 22:47:00 CST 2017), (六月, Thu Jun 01 22:47:00 CST 2017), (七月, Sat Jul 01 22:47:00 CST 2017), (八月, Tue Aug 01 22:47:00 CST 2017), (九月, Fri Sep 01 22:47:00 CST 2017), (十月, Sun Oct 01 22:47:00 CST 2017)], values=[¥100.00, ¥200.00], selectedIndex=5, summaryItemList=[SummaryListItem(tagName=晚餐, total=¥200.00)])]>
Expected :is <[SummaryDataViewState(points=[(0, 100.0), (5, 200.0)], months=[(五月, Mon May 01 22:47:00 CST 2017), (六月, Thu Jun 01 22:47:00 CST 2017), (七月, Sat Jul 01 22:47:00 CST 2017), (八月, Tue Aug 01 22:47:00 CST 2017), (九月, Fri Sep 01 22:47:00 CST 2017), (十月, Sun Oct 01 22:47:00 CST 2017)], values=[¥100.00, ¥200.00], selectedIndex=5, summaryItemList=[SummaryListItem(tagName=晚餐, total=¥200.00)])]>

Actual   :<[SummaryDataViewState(points=[(0, 100.0), (5, 200.0)], months=[(五月, Mon May 01 22:47:00 CST 2017), (六月, Thu Jun 01 22:47:00 CST 2017), (七月, Sat Jul 01 22:47:00 CST 2017), (八月, Tue Aug 01 22:47:00 CST 2017), (九月, Fri Sep 01 22:47:00 CST 2017), (十月, Sun Oct 01 22:47:00 CST 2017)], values=[¥100.00, ¥200.00], selectedIndex=5, summaryItemList=[SummaryListItem(tagName=晚餐, total=¥200.00)])]>

my test in here, can somebody solve this and tell me what's the problem?

Thientvse
  • 1,753
  • 1
  • 14
  • 23
littlegnal
  • 425
  • 4
  • 14
  • 1
    Can't see your assertion method, but I wonder if the `Calendar` instances are populated with very slightly un-equal values (like off by a few nanoseconds). – Ben P. Oct 26 '17 at 02:19
  • Or if you're using a shallow compare and you're comparing if the collections are equal, rather than the contents of the collections? – Paul Hicks Oct 26 '17 at 02:20
  • 2
    @BenP. I add `set(Calendar.MILLISECOND, 0)`, to solve this problem, the MILLISECOND not output in the `toString()` method, I was careless, thank you! – littlegnal Oct 26 '17 at 04:31

0 Answers0