Module: Spec::Rails::Mocks

Defined in:
lib/mocks.rb

Instance Method Summary collapse

Instance Method Details

#associated_model(class_name, stubs = {}) ⇒ Object

Shortcut to create a model that will be used in an association Example:

@model = MasterModel.create!
@mock = associated_model ChildModel, :some => :value
@model.children << @mock


31
32
33
# File 'lib/mocks.rb', line 31

def associated_model class_name, stubs = {}
  mock_model class_name, stubs.merge(:[]= => true, :save => true)
end

#invalid_model(class_name, stubs = {}) ⇒ Object

Shortcut to create a mock model that is prepared for a ActiveRecord::RecordInvalid.new(@model) call Equivalent to:

@model = mock_model Model, :some => :fields
prepare_for_errors_on @model


20
21
22
23
24
# File 'lib/mocks.rb', line 20

def invalid_model class_name, stubs = {}
  model = mock_model class_name, stubs
  prepare_for_errors_on model
  return model
end

#prepare_for_errors_on(model) ⇒ Object

Set up the given mock model in preparation for an ActiveRecord::RecordInvalid call For example:

@user = mock_model User
prepare_for_errors_on @user
@user.should_receive(:save!).and_raise(ActiveRecord::RecordInvalid.new(@user))

If you tried this without the call to prepare_for_errors_on, the RecordInvalid exception would not be raised.



10
11
12
13
14
# File 'lib/mocks.rb', line 10

def prepare_for_errors_on model
  errors = mock 'errors'
  errors.should_receive(:full_messages).and_return([])
  model.should_receive(:errors).and_return(errors)
end