dimanche 30 août 2015

RSpec less strict equality expectation for test

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:

  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.

Aucun commentaire:

Enregistrer un commentaire