如何为方法编写rspec

用户7354632781

我在可编程性文件夹中有以下代码

module Programmability
  module Parameter
    class Input < Parameter::Base
      attr_reader :args

  def initialize(name, type, **args)
    super(name, type)
    @args = args
  end

  def value=(val)
    if val
      val = convert_true_false(val)
--- some code ----
    end
    @value = val
  end      


  def convert_true_false(val)
    return val unless @args[:limit] == 1
    if [:char].include?(@type) && val == 'true'
      'Y'
    elsif [:char].include?(@type) && val == 'false'
      'N'
    elsif [:bit].include?(@type) && val == 'true'
      1
    elsif [:bit].include?(@type) && val == 'false'
      0
    end
  end
end
  end
end

我正在尝试为方法 convert_true_false 编写 rspec。我是 rspec 的新手。任何帮助表示赞赏。

我试过这样做

   context 'returns Y if passed true'
    input = Programmability::Parameter::Input.new(name, type, limit)

      it 'returns Y' do
        name = 'unregister_series'
        type = ':char'
        limit = 1

        expect(input.value = 'true' ).to eq('Y')
      end
  end

但它没有达到极限值。当它到达 convert_true_false 方法时,它就出来了,因为 @args[:limit] 为零

谢谢

恩年科夫

问题是 setter 总是返回传递的值:

class Test
  def foo=(foo)
    @foo = foo + 100
    return 42
  end
end

puts (Test.new.foo = 69) # 69

要解决它,只需在分配后检查值:

# instead of
expect(input.value = 'true').to eq 'Y'

# do
input.value = 'true'
expect(input.value).to eq 'Y'

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章