I have a class and specification.
class Store def activate(product_klass, product_id) product = product_klass.find(product_id) if product.inactive? product.update_attribute :active, true end end end describe Store do it "should activate an inactive product" do product = mock product.stub(:inactive?).and_return(true) store = Store.new store.activate(22)
Running the specification does not work . I get:
Mock received unexpected message :find_by_id with (1)
To satisfy this, I add product.should_receive(:find_by_id).with(1).and_return(product) before the line store.activate(product, 22) . (This seems wrong because I do not want my test to know too much about the internal methods that I am testing)
Running the spec again , I get a failure when the following line returns false instead of the expected true :
product.should be_active
So, it returns false , because product.update_attribute :active, true did not actually set active to true : it was just swallowed by the layout.
I have so many questions. How to go about rspec'cing? How should I check this? Am I using mocks and stub correctly?
Any help is greatly appreciated.
source share