0

I want those already methods to be tested, but everything I try, doesn't seem to fit with best practices nor do it work.

May be somebody can support me with this?

CODE To be tested

def any_subset_greater?
  divisors_sums.any?{|sum| sum > @value}
end

def no_subset_equal?
  !divisors_sums.any?{|sum| sum == @value}
end

def check_room
  any_subset_greater? && no_subset_equal?
end

RSPEC TRY

first specify seems not to set proper return values for the divisors method and the instance variable @value.

describe "#any_subset_greater?" do
  # Examples:
  # [1,2] > 4   #=> false
  # [1,2,3] > 4 #=> true
  specify "returns true, when value in the array is greater" do
    number.stub(:divisors){[1,2,3]}
    number.stub(:value) {4}
    expect(number.any_subset_greater?).to be_true
  end

end

describe "#no_subset_equal?" do
  # Examples:
  # 4 === [1,2,4]   #=> false
  # 4 === [1,2,3]   #=> false
  # 4 === [1,2,6]   #=> true
end

describe "#check_room" do
  # testing condition from methods above
end
radosch
  • 603
  • 7
  • 19
  • 1
    This looks like a fine shell, but what did you try for the describe methods? – Dave S. Feb 15 '13 at 18:37
  • 1
    Where does `divisor_sums` come from? Also, you shouldn't use a `mock` for the object under test as it doesn't contain your logic code. – Aaron K Feb 15 '13 at 18:41
  • @dave: the yes... I wanted to try that actually, but everything I do, seems to be fairly not right. when I stub the divisors with a value, Aaron: I don't know how... tried couple of things... any clue? could you post an example of mocking this? – radosch Feb 15 '13 at 18:58
  • 1
    It would be really helpful if you could post as much of the actual object class code. – Aaron K Feb 15 '13 at 19:12

1 Answers1

1

Without knowing how your object is setup, this answer is just a guess. I'm going to assume your object looks something like:

class SpecialNumber
  attr_reader :divisor_sums

  def initialize(n)
    @value = n
    # @divisor_sums is calculated here
  end

  # rest of your methods
end

So with this object in mind, the first set of tests could look like:

subject(:special_number) { SpecialNumber.new 4 }

describe "#any_subset_greater?" do
  context "no divisor sums greater than value" do
    it do
      special_number.stub(:divisor_sums).and_return [1, 2]

      expect(special_number.any_subset_greater?).to be_false
    end
  end

  context "one divisor sum greater than value" do
    it do
      special_number.stub(:divisor_sums).and_return [1, 2, 5]

      expect(special_number.any_subset_greater?).to be_true
    end
  end

  context "several divisor sums greater than value" do
    it do
      special_number.stub(:divisor_sums).and_return [1, 2, 5, 6]

      expect(special_number.any_subset_greater?).to be_true
    end
  end
end

But you don't have to stub this. If this is a simple class, simply creating a new object each time, which has expected divisors would be acceptable:

describe "#any_subset_greater?" do
  context "no divisor sums greater than value" do
    it do
      expect(SpecialNumber.new(3).any_subset_greater?).to be_false
    end
  end
end
Aaron K
  • 6,901
  • 3
  • 34
  • 29
  • hey, thank you very much! Is there a reason, why you use describe>context>it instead of describe>specify(with comment) I found out, that I stubbed it in a wrong place (facepalm). I just will post the solution which works for me below. – radosch Feb 16 '13 at 18:28
  • `specify` is an alias for `it`. To me using `context` provides semantic meaning. At the end of the day, it's a personal preference. Esp. for tests this straight forward. – Aaron K Feb 16 '13 at 19:20