Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我应该如何在ruby中拆分大型单元测试?_Ruby_Unit Testing_Testing - Fatal编程技术网

我应该如何在ruby中拆分大型单元测试?

我应该如何在ruby中拆分大型单元测试?,ruby,unit-testing,testing,Ruby,Unit Testing,Testing,我正在为我的一个rails模型编写一些单元测试。这是一个大型模型,在某些情况下,我对许多方法都有几十种断言。我更喜欢将普通测试/单元与rails的activesupport语法结合使用,因此我有一个如下文件: require 'test_helper' class ItemTest < ActiveSupport::TestCase test "for_sale? should be true if cond 1..." test "for_sale? should be true

我正在为我的一个rails模型编写一些单元测试。这是一个大型模型,在某些情况下,我对许多方法都有几十种断言。我更喜欢将普通测试/单元与rails的activesupport语法结合使用,因此我有一个如下文件:

require 'test_helper'
class ItemTest < ActiveSupport::TestCase
  test "for_sale? should be true if cond 1..."
  test "for_sale? should be true if cond 2..."
  test "for_sale? should be true if cond 3..."
  test "for_sale? should be true if cond 4..."
  test "for_sale? should be false if cond 1..."
  test "for_sale? should be false if cond 2..."
  test "for_sale? should be false if cond 3..."
  test "for_sale? should be false if cond 4..."
end
需要“测试助手”
类ItemTest
每种方法都是如此。问题是测试变得越来越复杂,因为我不能再依赖setup(),因为每组测试的上下文对于每个方法都有很大的不同。而且,如果不运行所有测试,就很难测试我正在使用的方法


其他人如何管理这样的大型测试用例?是否有最佳实践?

如果您想坚持使用测试/单元,我建议您使用shoulda。您不需要使用shoulda宏,但它将为您提供一个适用于此处的非常漂亮的特性:上下文。它允许您将单元测试分解为单独的部分,这些部分可以嵌套

context "as a really cool user" do
  test "test number one..."
end

context "as a lame user" do
  test "test number two..."
end

从那以后,您可以继续使用所有测试/单元语法,或者如果shoulda赢了您,您可以切换或混合n-match。

我不知道这是否是最佳实践,但下面是我要做的

我没有在
设置中创建我想玩的所有对象,而是有创建特定对象的方法,例如
创建信用卡已过期
创建信用卡被盗
创建信用卡有效
。有时我将这些方法放入一个模块中,以便它们可以在不同的测试用例类之间共享。我还创建自定义断言方法,例如
assert\u credit\u card\u rejected

所以我的示例代码是

require "test/unit"

module TestCreditCardHelper
  def assert_credit_card_rejected(credit_card, failure_message)
    assert credit_card.rejected?, failure_message #A real method might be more complicated
  end

  def create_credit_card_stolen
    CreditCard.new(:stolen => true)
  end

end

class TestCreditCard < Test::Unit::TestCase
  include TestCreditCardHelper

  def test_stolen_credit_card_rejected
    credit_card = create_credit_card_stolen
    assert_credit_card_rejected(credit_card, "Doesn't reject stolen credit cards")
  end
end
要求“测试/单元”
模块TestCreditCardHelper
def assert_信用卡被拒绝(信用卡,失败消息)
断言信用卡。拒绝?失败消息?实际方法可能更复杂
结束
def创建\u信用卡\u被盗
信用卡。新(:被盗=>真实)
结束
结束
类TestCreditCard
单元测试只能测试一件事。听起来这里的测试太多了。每个条件应进行一次测试

require 'test_helper'
class ItemTest_Condition1 < ActiveSupport::TestCase
  test "for_sale? should be true if cond 1..."
  test "for_sale? should be false if cond 1..."
end

class ItemTest_Condition2 < ActiveSupport::TestCase
  test "for_sale? should be true if cond 2..."
  test "for_sale? should be false if cond 2..."
end
class ItemTest_Condition3 < ActiveSupport::TestCase
  test "for_sale? should be true if cond 3..."
  test "for_sale? should be false if cond 3..."
end
...
需要“测试助手”
类ItemTest\u Condition1
另一个有用的心智模型是当其他变量…
时使用
context”。在前面提到的信用卡示例中,当信用卡被盗时,将
context“do…