-1

For example, I have a very simple model of a Card, for a popular card game. On initialize I want to give it a value and a suit (unique for every one of 36 cards). Here is my model:

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

I try to test it from console, and the result was satisfying:

c = Card.new
 => #<Card:0x007f8e34050428 @suit="H", @value="D"> 
2.0.0-p0 :024 > c.value
 => "D" 

But rspec tests don't give me the same result. Here they are:

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

And re result:

Card has @suit value on initialize
     Failure/Error: c.suit.should_not be nil

       expected not #<NilClass:8> => nil
                got #<NilClass:8> => nil

Where is my mistake?

3
  • 1
    c.suit.should_not be_nil Aug 27, 2013 at 6:31
  • expected: not nil got: nil Aug 27, 2013 at 6:51
  • 3
    What's in your spec_helper.rb? Do you define another Card class somewhere?
    – Jakob S
    Aug 27, 2013 at 7:09

1 Answer 1

1

If I put your code into a file and run it, I get no errors. What you have should work perfectly fine already - at least it does for me.

3
  • same error Card suit should not be nil Failure/Error: its(:suit) { should_not be_nil } expected: not nil got: nil I double check this in console and still getting this error. From a console I get 2.0.0-p0 :001 > c = Card.new => #<Card:0x007fefcc83fce8 @suit="B", @value="T"> Aug 27, 2013 at 7:02
  • 2
    I'm sorry, your original syntax was perfectly fine. In fact, your code works perfectly when I run it. No spec failures, just successes. Changing my answer to reflect this.
    – Jakob S
    Aug 27, 2013 at 7:07
  • You are right, I test it as an file, outside from rails ENV, and I get success :). the Jakob S comment maybe is the answer to this issue. Card class somewhere? – 1 hour ago Aug 27, 2013 at 8:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.