I have the following test:
it "can add an item" do
item = Item.new("car", 10000.00)
expect(@manager.add_item("car", 10000.00)).to eq(item)
end
Item's initialize looks like (class has attr_accessor for, type, price, and is_sold):
def initialize(type, price)
@type = type
@price = price
@is_sold = false
@@items << self
end
Manager's add item looks like:
def add_item(type, price)
Item.new(type, price)
end
This test is currently failing because the two items have different object ids, although their attributes are identical. Item's initialize method takes a type, and a price. I only want to check for equality on those features... Is there a way to test strictly for attribute equality?
I have tried should be, should eq, to be, and eql? with no luck.
Itemclass. In the code you've postedtypeandpriceare assigned to instance variables and RSpec (or any code outside the instance) has no way to access or compare those values. Does the class have e.g.attr_readerso other code can access them? - Jordan Runningitemand@manager.add_item("car", 10000.00))will be two different instances of the same classItem. If you want to test if the attributes are the same, leti0 = Item.new("car", 10000.00); i2 = Item.new("car", 10000.00), then testi0.type == i1.type, etc., - Cary Swoveland