예를 들어 인기있는 카드 게임을위한 카드의 간단한 모델이 있습니다. 초기화시 나는 가치와 정장 (36 장의 카드 하나 하나마다 고유)을주고 싶습니다. 여기 내 모델 :rspec으로 ActiveRecord 모델을 테스트하지 않는 방법은 무엇입니까?
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' –
'예상 : 0 이외 got : nil' –
spec_helper.rb에 무엇이 있습니까? 다른 'Card' 클래스를 어딘가에 정의합니까? –