-1

我对 Ruby 相当陌生,对 Rspec 来说是全新的,并且正在尝试使以下 Rspec 代码通过:

require "#{File.dirname(__FILE__)}/fish"

describe fish do

  before do
    @fish = fish.new(3)
  end

  it "should report the number of fish" do
    @fish.number.should equal 3
  end
end

我正在尝试测试以下代码,由于多种原因,我确信这是错误的,但现在我只是试图通过“错误数量的参数(1 代表 0)”错误“:

class fish
  def intialize n
    @number = n
  end
end
4

2 回答 2

1

您在类定义中拼写initialize错误(在 n 之后错过了 i。)

因此,您的类仍然具有默认构造函数,因为您尚未覆盖它。默认构造函数不接受任何参数,因此当您尝试传递时会抱怨3

于 2013-11-13T02:30:54.190 回答
1

我马上就注意到了几件事……

  1. 在您习惯 Ruby 的语法之前,我建议您使用括号。

  2. initialize你在课堂上拼错了。

  3. 用大写初始化Fish(和类) 。FishF


describe fish do
  before do
    @fish = Fish.new(3)
  end

  it "should report the number of fish" do
    @fish.number.should equal(3)
  end
end


class Fish
  def intialize n
    @number = n
  end
end
于 2013-11-13T02:35:05.300 回答