相关疑难解决方法(0)

我如何测试(TDD)单例类?

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

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

有没有办法强制处理单身元素,使其"新鲜"?

这是我的存储库代码:

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
Run Code Online (Sandbox Code Playgroud)

测试:

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] …
Run Code Online (Sandbox Code Playgroud)

ruby tdd minitest

2
推荐指数
1
解决办法
575
查看次数

标签 统计

minitest ×1

ruby ×1

tdd ×1