0
class UbacParser
    def initialize(str)
        @str= str
        @valid= true
        base_parse
    end

    private
    def base_parse
        @protocol_code = Integer(@str[0..2]) rescue nil
        begin
        @data = @str[@str.index('<')+1..@str.index('>')-1] 
        str_mod = @str[@str.index('>##')+1..-1] 

        arr_mod=str_mod[2..-3].split(',')
        @hash_mod=Hash.new
        arr_mod.each_index { |i| @hash_mod[arr_mod[i].split('=')[0]]=arr_mod[i].split('=')[1] }
        rescue
            puts "error in data_parse"
            @valid=false
        end
    end

    public
    def valid?

        return @valid;
    end

    def [](key)
        unless @valid: return 
        end
        @hash_mod[key.upcase]
    end

end

我如何为此 Ruby 类代码编写 rspec 验证器/测试用例?

请帮忙

4

1 回答 1

0

我建议您查看rspec 网站,然后通过先编写规范从头开始。这将是一个很好的学习练习,并帮助您编写可测试的代码。

于 2010-08-11T08:19:21.193 回答