[英]wrong number of arguments ruby rspec
我正在嘗試使用rspec為我的代碼編寫單元測試。 我不斷收到“參數數量錯誤”錯誤:
class MyClass
attr_accessor :env, :company,:size, :role, :number_of_hosts,:visability
def initialize(env, company, size, role, number_of_hosts, visability)
@env, @company, @size, @role, @number_of_hosts, @visability = env, company, size, role, number_of_hosts, visability
end
end
這是我的測試:
require_relative "../lib/MyClass.rb"
describe MyClass do
it "has an environment" do
MyClass.new("environment").env.should respond_to :env
end
it "has a company" do
MyClass.new("company").company.should respond_to :company
end
...
當我運行rspec時,我得到:
1) MyClass has an environment
Failure/Error: MyClass.new("environment").env.should respond_to :env
ArgumentError:
wrong number of arguments (1 for 6)
# ./lib/MyClass.rb:4:in `initialize'
# ./spec/MyClass_spec.rb:5:in `new'
# ./spec/MyClass_spec.rb:5:in `block (2 levels) in <top (required)>'
...
我想念什么?
編輯
塞爾吉奧幫助了...但是
塞爾吉奧的答案奏效了……盡管我還有一個問題:
給定班級:
class Team
attr_accessor :name, :players
def initialize(name, players = [])
raise Exception unless players.is_a? Array
@name = name
raise Exception if @name && has_bad_name
@players = players
end
def has_bad_name
list_of_words = %w{crappy bad lousy}
list_of_words - @name.downcase.split(" ") != list_of_words
end
def favored?
@players.include? "George Clooney"
end
end
和規格...
require_relative "../lib/team.rb"
describe Team do
it "has a name" do
Team.new("random name").should respond_to :name
end
it "has a list of players" do
Team.new("random name").players.should be_kind_of Array
end
...
測試通過,沒有相同的錯誤...(工作正常:Team.new(“ random name”))
有什么解釋嗎?
這是錯誤MyClass.new("environment")
。 如您所定義的def initialize(env, company, size, role, number_of_hosts, visability)
。 因此,在調用MyClass#new
方法時應傳遞6
參數。 但是實際上,您只會傳遞一個"environment"
。 因此,您得到了合法的錯誤- 錯誤的參數數量(1表示6) 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.