Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.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_Oop_Testing - Fatal编程技术网

Ruby 如何检查类是否正确实例化

Ruby 如何检查类是否正确实例化,ruby,oop,testing,Ruby,Oop,Testing,假设我有一门课: class Product def initialize(v) @var = v end end 我想用RSpec测试类的实例化是否正常。它应该在类或单元测试中进行测试,我该怎么做?您需要向类添加属性读取器,以便通过RSpec、Test::Unit、Minitest或其他任何方式验证其状态。但是,您所说的基本上是测试Ruby作为一种语言是否正常工作,这确实不是您需要关心的事情。您需要向类添加一个属性读取器,以便通过RSpec、Test::Unit、Minite

假设我有一门课:

class Product
  def initialize(v)
    @var = v
  end
end

我想用RSpec测试类的实例化是否正常。它应该在类或单元测试中进行测试,我该怎么做?

您需要向类添加属性读取器,以便通过RSpec、Test::Unit、Minitest或其他任何方式验证其状态。但是,您所说的基本上是测试Ruby作为一种语言是否正常工作,这确实不是您需要关心的事情。

您需要向类添加一个属性读取器,以便通过RSpec、Test::Unit、Minitest或其他任何方式验证其状态。然而,您所说的基本上是测试Ruby作为一种语言是否正常工作,这确实不是您需要关心的事情。

如果您的初始值设定项如此简单,那么测试它是不值得的。 另一方面,如果要在初始值设定项中添加一些参数检查或其他逻辑,那么最好对其进行测试

大多数情况下,在这种情况下,最好的做法是在参数错误时引发IllegalArgumeInterror。在这种情况下,您可以确保初始化对象确实(或没有)引发错误

如果您正在做更复杂的事情,您可能需要检查实例变量的值。我不认为使用
attr\u reader
是一个好主意,因为我认为为测试目的更改类实现是一个坏主意。相反,我将使用
#instance_variable_get
读取变量

class Foo
  def initialize(mandatory_param, optional_param = nil)
    raise IllegalArgumentError.new("param cannot be #{param}") if mandatory_param == 42
    @var1 = mandatory_param
    @var2 = optional_param unless param.is_a? String
  end
end

describe Foo do
  it "should not accept 42 as an argument" do
    expect { Foo.new(42, 'hello') }.to raise_error(IllegalArgumentError)
  end

  it "should set var2 properly if it's not a String" do
    f = Foo.new('hello', 1)
    f.instance_variable_get(:@var2).should eq 1
  end

  it "should not set var2 if it's a String" do
    f = Foo.new('hello', 'world')
    f.instance_variable_get(:@var2).should be_nil
  end
end

如果您的初始值设定项如此简单,那么不值得对其进行测试。 另一方面,如果要在初始值设定项中添加一些参数检查或其他逻辑,那么最好对其进行测试

大多数情况下,在这种情况下,最好的做法是在参数错误时引发IllegalArgumeInterror。在这种情况下,您可以确保初始化对象确实(或没有)引发错误

如果您正在做更复杂的事情,您可能需要检查实例变量的值。我不认为使用
attr\u reader
是一个好主意,因为我认为为测试目的更改类实现是一个坏主意。相反,我将使用
#instance_variable_get
读取变量

class Foo
  def initialize(mandatory_param, optional_param = nil)
    raise IllegalArgumentError.new("param cannot be #{param}") if mandatory_param == 42
    @var1 = mandatory_param
    @var2 = optional_param unless param.is_a? String
  end
end

describe Foo do
  it "should not accept 42 as an argument" do
    expect { Foo.new(42, 'hello') }.to raise_error(IllegalArgumentError)
  end

  it "should set var2 properly if it's not a String" do
    f = Foo.new('hello', 1)
    f.instance_variable_get(:@var2).should eq 1
  end

  it "should not set var2 if it's a String" do
    f = Foo.new('hello', 'world')
    f.instance_variable_get(:@var2).should be_nil
  end
end