Reputation: 603
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
Upvotes: 0
Views: 2275
Reputation: 6961
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
Upvotes: 1