【发布时间】:2013-08-27 06:27:37
【问题描述】:
例如,我有一个非常简单的纸牌模型,用于流行的纸牌游戏。在初始化时,我想给它一个值和一套花色(36 张牌中的每一张都是唯一的)。这是我的模型:
class Card
attr_accessor :suit, :value
@@values = [6, 7, 8, 9, 10, 'B', 'D', 'K', 'T'] * 4
@@suites = ['B', 'C', 'P', 'H'] * 9
def initialize
@suit = get_rundom_suit_or_value @@suites
@value = get_rundom_suit_or_value @@values
end
private
def get_rundom_suit_or_value given_array
given_array.delete_at(given_array.index(given_array.sample)) unless given_array.nil? || given_array.empty?
end
end
我尝试从控制台测试它,结果令人满意:
c = Card.new
=> #<Card:0x007f8e34050428 @suit="H", @value="D">
2.0.0-p0 :024 > c.value
=> "D"
但是 rspec 测试并没有给我相同的结果。他们在这里:
require 'spec_helper'
describe 'Card' do
it 'has @suit value on initialize' do
c = Card.new
c.suit.should_not be nil
end
it 'has @value value on initialize' do
c = Card.new
c.value.should_not be nil
end
end
再结果:
Card has @suit value on initialize
Failure/Error: c.suit.should_not be nil
expected not #<NilClass:8> => nil
got #<NilClass:8> => nil
我的错误在哪里?
【问题讨论】:
-
c.suit.should_not be_nil -
expected: not nil got: nil -
spec_helper.rb 中有什么内容?您是否在某处定义了另一个
Card类?