Ruby / Minitest规范:如何测试方法是否在给定数组中返回值?

时间:2019-05-26 14:35:06

标签: ruby minitest

我有以下课程:

  class State
    def self.alive
      :alive
    end

    def self.dead
      :dead
    end
  end

测试很简单:

it 'should report the expected state for alive' do
  State.alive.must_equal :alive
end

it 'should report the expected state for dead' do
  State.dead.must_equal :dead
end

我想添加一个返回随机状态的类方法。

class State
# ...
    def self.random
      [alive, dead].sample
    end

但是,我不确定我需要使用哪个断言。

目前,我正在对其进行如下测试:

  it 'should return a random state' do
    %i[dead alive].must_include State.random
  end

哪个回到了前面。上面的测试正在测试文字数组而不是State类。

是否有更好的方法来测试方法是否返回指定数组中包含的值?

1 个答案:

答案 0 :(得分:2)

在向您提供解决方案之前,我想先了解一些有关Spec DSL和Minitest的细节。 par(mfrow = c(2, 2)) # First Plot: Passung auf Zufriedenheit p1 <- ggplot(df.hlm_cc_select, aes(zPssg_sd, zZufri)) p1 + geom_jitter(aes(colour = v_187_corr))+ labs(title="Zufriedenheit erklärt durch Direktheit",x="Direktheit", y = "Zufriedenheit") + scale_color_viridis(discrete = TRUE, option = "A")+ scale_fill_viridis(discrete = TRUE) + theme_dark() + theme(axis.line.x = element_line(colour = 'black', size=0.5, linetype='solid'), axis.line.y = element_line(colour = 'black', size=0.5, linetype='solid')) + #geom_smooth(aes(x = zagree_group_withinmeet_levelrat, y = zZufri), inherit.aes = FALSE, se = FALSE)+ geom_smooth(method=lm, color="black") # Second Plot: Homogenität Problemart auf Zufriedenheit p2 <- ggplot(df.hlm_cc_select, aes(zmean.aggr.prb_sd, zZufri)) p2 + geom_jitter(aes(colour = v_187_corr))+ labs(title="Zufriedenheit erklärt durch Homogenität (Problemart)",x="Homogenität (Problemart)", y = "Zufriedenheit") + scale_color_viridis(discrete = TRUE, option = "A")+ scale_fill_viridis(discrete = TRUE) + theme_dark() + theme(axis.line.x = element_line(colour = 'black', size=0.5, linetype='solid'), axis.line.y = element_line(colour = 'black', size=0.5, linetype='solid')) + #geom_smooth(aes(x = zagree_group_withinmeet_levelrat, y = zZufri), inherit.aes = FALSE, se = FALSE)+ geom_smooth(method=lm, color="black") # Third Plot: Homogenität Level auf Zufriedenheit p3 <- ggplot(df.hlm_cc_select, aes(zagree_group_withinmeet_levelrat, zZufri)) p3 + geom_jitter(aes(colour = v_187_corr))+ labs(title="Satisfaction explained by Homogeneity (Level)",x="Homogeneity (Level)", y = "Satisfaction") + scale_color_viridis(discrete = TRUE, option = "A")+ scale_fill_viridis(discrete = TRUE) + theme_dark() + theme(axis.line.x = element_line(colour = 'black', size=0.5, linetype='solid'), axis.line.y = element_line(colour = 'black', size=0.5, linetype='solid')) + #geom_smooth(aes(x = zagree_group_withinmeet_levelrat, y = zZufri), inherit.aes = FALSE, se = FALSE)+ geom_smooth(method=lm, color="black") # Forth Plot: Intensität auf Zufriedenheit p4 <- ggplot(df.hlm_cc_select, aes(zmeans_levelsums, zZufri)) p4 + geom_jitter(aes(colour = v_187_corr))+ labs(title="Zufriedenheit erklärt durch Intensität",x="Intensität", y = "Zufriedenheit") + scale_color_viridis(discrete = TRUE, option = "A")+ scale_fill_viridis(discrete = TRUE) + theme_dark() + theme(axis.line.x = element_line(colour = 'black', size=0.5, linetype='solid'), axis.line.y = element_line(colour = 'black', size=0.5, linetype='solid')) + #geom_smooth(aes(x = zagree_group_withinmeet_levelrat, y = zZufri), inherit.aes = FALSE, se = FALSE)+ geom_smooth(method=lm, color="black") 方法是must_include断言的Spec DSL期望。该方法具有以下签名:

assert_includes

创建期望时,签名为:

assert_includes collection, obj, msg = nil

因此,您真正要问的是一种以相反的参数顺序调用这些方法的方法。通过创建使用所需参数顺序的新方法,这非常简单。首先,我们必须创建断言:

Collection#must_include obj, msg = nil

现在有了断言方法,我们可以创建Spec DSL期望:

module Minitest::Assertions
  ##
  # Fails unless +obj+ is included in +collection+.

  def assert_included_in obj, collection, msg = nil
    msg = message(msg) {
      "Expected #{mu_pp(obj)} to be included in #{mu_pp(collection)}"
    }
    assert_respond_to collection, :include?
    assert collection.include?(obj), msg
  end
end

现在我们已经定义了期望及其使用的断言,我们可以在测试中使用它:

module Minitest::Expectations
  ##
  # See Minitest::Assertions#assert_included_in
  #
  #    collection.must_be_one_of obj
  #
  # :method: must_be_one_of

  infect_an_assertion :assert_included_in, :must_be_one_of, :reverse
end

我会更进一步,并使用monad值来调用期望值:

it "should return a random state" do
  State.random.must_be_one_of %i[dead alive]
end