I am doing some simple TDD and I'm looking to test some elements in an array that I have, and I'm having a little bit of trouble. Here's what I have thus far as far as my tests:
require_relative '../divisible'
describe Divisible do
before do
subject = Divisible.new
end
describe 'find_numbers' do
it "stores a number divisible by 3 and 5 in an array" do
subject.find_numbers(3).should == 1
subject.find_numbers(6).should == 3
subject.find_numbers(12).should == 6
end
it "calls the sum_numbers function" do
subject.find_numbers(3).should_receive(:sum_numbers).with(3)
end
end
end
Here is my class:
class Divisible
def find_numbers(num)
divisible_numbers = Array.new
for i in 1..num
if (i % 3) == 0 || (i % 5) == 0
divisible_numbers.push(i)
end
end
sum_numbers(divisible_numbers)
return divisible_numbers.count
end
def sum_numbers(array)
end
end
Ok, so what I want is to test the elements in the divisible_numbers array. I'm having some trouble accessing that array from the test to verify that the numbers I want are actually being stored in that array. Any suggestions as to how to test this? I realize that those first tests will need to be re-written, but I'm hoping that by testing the array itself, then I don't have to worry about the tests breaking down the road.
Side note:
I'm also looking for a way to be sure that my find_numbers function calls my sum_numbers function with rspec. Ideally I would like to test that the function would be called before having to create the sum_numbers function. It seems as though I would be using a stub, but that concept has not fully settled with me, so any pointers here would be awesome! Thanks.