Module Spec::Rails::Mocks
In: lib/spec/rails/mocks.rb

Methods

Public Instance methods

[Source]

    # File lib/spec/rails/mocks.rb, line 81
81:             def as_new_record
82:               self.id = nil
83:               self
84:             end

[Source]

    # File lib/spec/rails/mocks.rb, line 75
75:             def connection
76:               raise Spec::Rails::IllegalDataAccessException.new("stubbed models are not allowed to access the database")
77:             end

Creates a mock object instance for a model_class with common methods stubbed out. Additional methods may be easily stubbed (via add_stubs) if stubs is passed.

[Source]

    # File lib/spec/rails/mocks.rb, line 11
11:       def mock_model(model_class, options_and_stubs = {})
12:         id = next_id
13:         options_and_stubs.reverse_merge!({
14:           :id => id,
15:           :to_param => id.to_s,
16:           :new_record? => false,
17:           :errors => stub("errors", :count => 0)
18:         })
19:         m = mock("#{model_class.name}_#{options_and_stubs[:id]}", options_and_stubs)
20:         m.send(:__mock_proxy).instance_eval "def @target.is_a?(other)\n\#{model_class}.ancestors.include?(other)\nend\ndef @target.kind_of?(other)\n\#{model_class}.ancestors.include?(other)\nend\ndef @target.instance_of?(other)\nother == \#{model_class}\nend\ndef @target.class\n\#{model_class}\nend\n"
21:         yield m if block_given?
22:         m
23:       end

[Source]

    # File lib/spec/rails/mocks.rb, line 78
78:             def new_record?
79:               id.nil?
80:             end

Creates an instance of Model that is prohibited from accessing the database. For each key in hash_of_stubs, if the model has a matching attribute (determined by asking it, which it answers based on schema.rb) are simply assigned the submitted values. If the model does not have a matching attribute, the key/value pair is assigned as a stub return value using RSpec‘s mocking/stubbing framework.

new_record? is overridden to return the result of id.nil? This means that by default new_record? will return false. If you want the object to behave as a new record, sending it as_new_record will set the id to nil. You can also explicitly set :id => nil, in which case new_record? will return true, but using as_new_record makes the example a bit more descriptive.

While you can use stub_model in any example (model, view, controller, helper), it is especially useful in view examples, which are inherently more state-based than interaction-based.

Examples

  stub_model(Person)
  stub_model(Person).as_new_record
  stub_model(Person, :id => 37)
  stub_model(Person) do |person|
    model.first_name = "David"
  end

[Source]

    # File lib/spec/rails/mocks.rb, line 70
70:       def stub_model(model_class, stubs = {})
71:         stubs = {:id => next_id}.merge(stubs)
72:         returning model_class.new do |model|
73:           model.id = stubs.delete(:id)
74:           (class << model; self; end).class_eval do
75:             def connection
76:               raise Spec::Rails::IllegalDataAccessException.new("stubbed models are not allowed to access the database")
77:             end
78:             def new_record?
79:               id.nil?
80:             end
81:             def as_new_record
82:               self.id = nil
83:               self
84:             end
85:           end
86:           stubs.each do |k,v|
87:             if model.has_attribute?(k)
88:               model[k] = stubs.delete(k)
89:             end
90:           end
91:           add_stubs(model, stubs)
92:           yield model if block_given?
93:         end
94:       end

[Validate]