Module: RailsExtension::ActiveSupportExtension::TestCase::InstanceMethods

Defined in:
lib/rails_extension/active_support_extension/test_case.rb

Instance Method Summary collapse

Instance Method Details

#assert_changes(expression, message = nil, &block) ⇒ Object

basically a copy of assert_difference, but without any explicit comparison as it is simply stating that something will change (designed for updated_at)



182
183
184
185
186
187
188
189
190
191
192
# File 'lib/rails_extension/active_support_extension/test_case.rb', line 182

def assert_changes(expression, message = nil, &block)
  b = block.send(:binding)
  exps = Array.wrap(expression)
  before = exps.map { |e| eval(e, b) }
  yield
  exps.each_with_index do |e, i|
    error = "#{e.inspect} didn't change"
    error = "#{message}.\n#{error}" if message
    assert_not_equal(before[i], eval(e, b), error)
  end
end

#deny_changes(expression, message = nil, &block) ⇒ Object

Just a negation of assert_changes



195
196
197
198
199
200
201
202
203
204
205
# File 'lib/rails_extension/active_support_extension/test_case.rb', line 195

def deny_changes(expression, message = nil, &block)
  b = block.send(:binding)
  exps = Array.wrap(expression)
  before = exps.map { |e| eval(e, b) }
  yield
  exps.each_with_index do |e, i|
    error = "#{e.inspect} changed"
    error = "#{message}.\n#{error}" if message
    assert_equal(before[i], eval(e, b), error)
  end
end

#method_missing_with_create_object(symb, *args, &block) ⇒ Object



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/rails_extension/active_support_extension/test_case.rb', line 142

def method_missing_with_create_object(symb,*args, &block)
  method = symb.to_s
#     if method =~ /^create_(.+)(\!?)$/
  if method =~ /^create_([^!]+)(!?)$/
    factory = if( $1 == 'object' )
# doesn't work for controllers yet.  Need to consider
# singular and plural as well as "tests" method.
# Probably should just use the explicit factory
# name in the controller tests.
#       self.class.name.sub(/Test$/,'').underscore
      model_name.underscore
    else
      $1
    end
    bang = $2
    options = args.extract_options!
    if bang.blank?
      record = Factory.build(factory,options)



#         record.attributes = options # occassionally needed (study_subject_id)



      record.save
      record
    else
      Factory(factory,options)
    end
  else
#       super(symb,*args, &block)
    method_missing_without_create_object(symb,*args, &block)
  end
end

#model_nameObject



136
137
138
139
140
# File 'lib/rails_extension/active_support_extension/test_case.rb', line 136

def model_name
#     self.class.name.sub(/Test$/,'')
#     self.class.name.demodulize.sub(/Test$/,'')
  self.class.st_model_name
end