我是新的ruby代碼。我的團隊成員編寫類甲板實現遊戲設置爲什麼我不能在我的新rspec版本中使用期望值相等
class Deck
@array = Array.new
# create a deck of n cards
# n <= 0 returns empty array.
def initialize (n=1)
@array = (0..n-1).to_a
end
end
我嘗試寫rspec的測試,這也是新的給我們,這裏的測試代碼:
#!/user/bin/ruby -w
require '../deck'
require 'rspec/expectations'
describe "Deck#new" do
context "with one parameter " do
it "has parameter n = 0" do
expect(Deck.new(0)).to match_array([])
end
it "has parameter n = 1" do
expect(Deck.new(1)).to eq([0])
end
it "has parameter n = 5" do
expect(Deck.new(5))==([0,1,2,3,4])
end
it "has parameter n<0" do
expect(Deck.new(-1))==([])
end
end
end
但是當我運行該測試,它給我
expected a collection that can be converted to an array with `#to_ary` or `#to_a`, but got #<Deck:0xb82edb74 @array=[]>
和前兩個失敗,我不明白。我在代碼中錯過了什麼嗎?感謝幫助。我的rspec版本是最新的。
肯定。我以爲我們不應該使用運營商? @Stefan – Echo 2015-02-05 16:10:22
我不認爲'expect(a)== b'正在做任何事情。 [正確的語法](https://www.relishapp.com/rspec/rspec-expectations/v/3-2/docs/built-in-matchers/equality-matchers#compare-using- (a).to == b' – Stefan 2015-02-05 16:17:50
順便說一句,代替'(0..n-1)',你可以使用'(0 ... n)'(3個點) – Stefan 2015-02-05 16:19:06