If you are using flexmock for testing your rails code and you are mocking an active record model using flexmock(:model, Model), you may get an error like
undefined method `destroyed?'
The problem is that flexmock defines a lot of methods/attributes like id and new_instance? etc but does not define a destroyed? method that rails now expects. Fortunately, this is easy to fix. Create a file ‘config/initializers/flexmock_extensions.rb’ and put this in
class FlexMock class MockContainerHelper def add_model_methods(mock, model_class, id) container = mock.flexmock_container mock_errors = container.flexmock("errors") mock_errors.should_receive(:count).and_return(0).by_default mock_errors.should_receive(:full_messages).and_return([]).by_default mock.should_receive(:id).and_return(id).by_default mock.should_receive(:to_params).and_return(id.to_s).by_default mock.should_receive(:new_record?).and_return(false).by_default mock.should_receive(:class).and_return(model_class).by_default mock.should_receive(:errors).and_return(mock_errors).by_default mock.should_receive(:destroyed?).and_return(false).by_default # HACK: Ruby 1.9 needs the following lambda so that model_class # is correctly bound below. lambda { } mock.should_receive(:is_a?).with(any).and_return { |other| other == model_class }.by_default mock.should_receive(:instance_of?).with(any).and_return { |other| other == model_class }.by_default mock.should_receive(:kind_of?).with(any).and_return { |other| model_class.ancestors.include?(other) }.by_default end end end
Basically, we are defining the method destroyed? and returning false everytime. Now your tests should work