1

Im trying to pass a test for pushing an item into an array from another class. Here is the method.

require_relative 'message'

class Test

  attr_reader :message

  def initialize(message = Message.new)
    @message = message
  end

  def push(hello)
    @message.array << hello
  end
end

The empty array in a different class.

class Message

  attr_reader :array 

  def initialize
    @array = []
  end
end

and my test.

require 'test'

describe Test do

  let(:message) { double(array: [])}

  describe '#push' do
    it 'pushes an item into an array from the message class' do
      subject.push("hello")
      expect(message.array).to eq ["hello"]
    end
  end
end

currenty getting the error

expected: ["hello"]
     got: []

       (compared using ==)

what am i doing wrong? The method itself is simple and works, why does my test not?

2 Answers 2

1

The message you defined here : let(:message) { double(array: [])} has nothing to do with the rest.

Since you pushed into subject you have to check on it.

require 'test'

describe Test do
  describe '#push' do
    it 'pushes an item into an array from the message class' do
      subject = Test.new
      subject.push("hello")
      expect(subject.message.array).to eq ["hello"]
    end
  end
end
Sign up to request clarification or add additional context in comments.

2 Comments

ahhhhh can't belive i missed that! thanks so much dude ;)
You're welcome :) (Don't hesitate to flag it as the good answer if it solved your problem)
0

In your let, you assign an empty array to message and then test if it contains 'hello'. That fails because message is empty. Did you mean to push 'hello' to the message array instead of to 'subject'?

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.