如何测试(TDD)单身人士课程? [英] How do I test (TDD) a singleton class?

查看:90
本文介绍了如何测试(TDD)单身人士课程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Minitest在Ruby应用程序中从DDD和TDD开始. 我创建了一个存储库类(没有数据库访问权限,但是它为我生成了实体).这是一个单身人士.

I am starting with DDD and TDD in a Ruby application using Minitest. I created a repository class (no database access, but it generates the entities for me). It is a singleton.

我想测试实体的生成.问题在于,因为它是单例,所以测试的执行顺序会影响结果.

I would like to test the generation of the entities. The problem is that because it is a singleton, the order of executions of the tests affect the results.

有什么方法可以强制处理单例元素,使它新鲜"吗?

Is there any way to force the disposal of the singleton element so it is "fresh"?

这是我的存储库代码:

require "singleton"

class ParticipantRepository
    include Singleton

    def initialize()
        @name_count = 0
    end

    def generate_participant()
        participant = Participant.new
        participant.name = "Employee#{get_name_count()}"
        return participant
    end

    private 
    def get_name_count()
        old_name_count = @name_count
        @name_count += 1
        return old_name_count
    end
end

测试:

require_relative 'test_helper'


class ParticipantRepositoryTest < MiniTest::Unit::TestCase

    def setup()
        @repository = ParticipantRepository.instance
    end

    def test_retrieve_participant
       participant = @repository.generate_participant

       refute_nil participant
       refute_nil participant.name
       refute_equal("", participant.name)
       assert_equal(0, participant.subordinates_count)
    end

    def test_employee_name_increment
        participant1 = @repository.generate_participant
        participant2 = @repository.generate_participant

        refute_equal(participant1.name, participant2.name)

        index_participant1 = /Employee([0-9]+)/.match(participant1.name)[1]
        index_participant2 = /Employee([0-9]+)/.match(participant2.name)[1]

        assert_equal(0, index_participant1.to_i)
        assert_equal(1, index_participant2.to_i)
    end

end

断言assert_equal(0, index_participant1.to_i)在首先执行test_employee_name_increment时成功,而在最后执行时失败.

The assertion assert_equal(0, index_participant1.to_i) succeeds when test_employee_name_increment is executed first and fails if it is executed last.

我希望能够测试存储库(因为它将演变成更大的存储库).我该怎么办?

I would like to be able to test the repository (because it will evolve into something bigger). How can I do that?

谢谢!

推荐答案

订购测试无所谓.要正确测试单例类,您需要将其视为实例对象.为此,请在设置过程中将您的单身人士包裹在一个匿名类中.每次调用安装程序时,您都会获得一个完整的ParticipantRepository副本:

Ordering your tests won't matter. To properly test a singleton class you need to treat it like an instance object. To do that, wrap your singleton in an anonymous Class during setup. Every time setup is called you'll get an untouched copy of the ParticipantRepository:

def setup
  @repository = Class.new(ParticipantRepository).instance
end

这篇关于如何测试(TDD)单身人士课程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆