Module: RSpec::Matchers

Defined in:
lib/rspec2-rails-views-matchers.rb

Constant Summary

DATE_FIELD_TYPES =
%w( date month week time datetime datetime-local )

Instance Method Summary collapse

Instance Method Details

#have_form(action_url, method, options = {}, &block) ⇒ Object



232
233
234
235
236
237
238
239
# File 'lib/rspec2-rails-views-matchers.rb', line 232

def have_form action_url, method, options={}, &block
  options[:with] ||= {}
  id = options[:with].delete(:id)
  tag = 'form'; tag += '#'+id if id
  options[:with].merge!(:action => action_url)
  options[:with].merge!(:method => method.to_s)
  have_tag tag, options, &block
end

#have_tag(tag, options = {}) { ... } ⇒ Object

have_tag matcher

Examples:

rendered.should have_tag('div')
rendered.should have_tag('h1.header')
rendered.should have_tag('div#footer')
rendered.should have_tag('input#email', :with => { :name => 'user[email]', :type => 'email' } )
rendered.should have_tag('div', :count => 3)            # matches exactly 3 'div' tags
rendered.should have_tag('div', :count => 3..7)         # something like have_tag('div', :minimum => 3, :maximum => 7)
rendered.should have_tag('div', :minimum => 3)          # matches more(or equal) than 3 'div' tags
rendered.should have_tag('div', :maximum => 3)          # matches less(or equal) than 3 'div' tags
rendered.should have_tag('p', :text => 'some content')  # will match "<p>some content</p>"
rendered.should have_tag('p', :text => /some content/i) # will match "<p>sOme cOntEnt</p>"
rendered.should have_tag('textarea', :with => {:name => 'user[description]'}, :text => "I like pie")
"<html>
  <body>
    <h1>some html document</h1>
  </body>
 </html>".should have_tag('body') { with_tag('h1', :text => 'some html document') }
'<div class="one two">'.should have_tag('div', :with => { :class => ['two', 'one'] })
'<div class="one two">'.should have_tag('div', :with => { :class => 'two one' })

Parameters:

  • tag (String)

    css selector for tag you want to match

  • options (Hash) (defaults to: {})

    options hash(see below)

Options Hash (options):

  • :with (Hash)

    hash with other attributes, within this, key :class have special meaning, you may specify it as array of expected classes or string of classes separated by spaces, order does not matter

  • :count (Fixnum)

    count of matched tags(DO NOT USE :count with :minimum(:min) or :maximum(:max)*)

  • :count (Range)

    count of matched tags should be between range minimum and maximum values

  • :minimum (Fixnum)

    minimum count of elements to match

  • :min (Fixnum)

    same as :minimum

  • :maximum (Fixnum)

    maximum count of elements to match

  • :max (Fixnum)

    same as :maximum

Yields:

  • block where you should put with_tag



209
210
211
212
213
214
# File 'lib/rspec2-rails-views-matchers.rb', line 209

def have_tag tag, options={}, &block
  if options.kind_of? String
    options = { :text => options }
  end
  @__current_scope_for_nokogiri_matcher = NokogiriMatcher.new(tag, options, &block)
end

#with_button(text, value = nil, options = {}) ⇒ Object



433
434
435
436
437
438
439
440
441
442
# File 'lib/rspec2-rails-views-matchers.rb', line 433

def with_button text, value=nil, options={}
  options[:with] ||= {}
  if value.is_a?(Hash)
    options.merge!(value)
    value=nil
  end
  options[:with].merge!(:value => value.to_s) if value
  options.merge!(:text => text) if text
  @__current_scope_for_nokogiri_matcher.should have_tag('button', options)
end

#with_checkbox(name, value = nil) ⇒ Object



361
362
363
364
365
# File 'lib/rspec2-rails-views-matchers.rb', line 361

def with_checkbox name, value=nil
  options = { :with => { :name => name, :type => 'checkbox' } }
  options[:with].merge!(:value => value) if value
  should_have_input(options)
end

#with_date_field(date_field_type, name = nil, options = {}) ⇒ Object



315
316
317
318
319
320
321
# File 'lib/rspec2-rails-views-matchers.rb', line 315

def with_date_field date_field_type, name=nil, options={}
  date_field_type = date_field_type.to_s
  raise "unknown type `#{date_field_type}` for date picker" unless DATE_FIELD_TYPES.include?(date_field_type)
  options = { :with => { :type => date_field_type.to_s }.merge(options.delete(:with)||{}) }
  options[:with].merge!(:name => name.to_s) if name
  should_have_input(options)
end

#with_email_field(name, value = nil) ⇒ Object



265
266
267
268
269
# File 'lib/rspec2-rails-views-matchers.rb', line 265

def with_email_field name, value=nil
  options = { :with => { :name => name, :type => 'email' } }
  options[:with].merge!(:value => value) if value
  should_have_input(options)
end

#with_file_field(name) ⇒ Object



341
342
343
344
# File 'lib/rspec2-rails-views-matchers.rb', line 341

def with_file_field name
  options = { :with => { :name => name, :type => 'file' } }
  should_have_input(options)
end

#with_hidden_field(name, value = nil) ⇒ Object



241
242
243
244
245
# File 'lib/rspec2-rails-views-matchers.rb', line 241

def with_hidden_field name, value=nil
  options = { :with => { :name => name, :type => 'hidden' } }
  options[:with].merge!(:value => value) if value
  should_have_input(options)
end

#with_number_field(name, value = nil) ⇒ Object



289
290
291
292
293
# File 'lib/rspec2-rails-views-matchers.rb', line 289

def with_number_field name, value=nil
  options = { :with => { :name => name, :type => 'number' } }
  options[:with].merge!(:value => value.to_s) if value
  should_have_input(options)
end

#with_option(text, value = nil, options = {}) ⇒ Object



401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
# File 'lib/rspec2-rails-views-matchers.rb', line 401

def with_option text, value=nil, options={}
  options[:with] ||= {}
  if value.is_a?(Hash)
    options.merge!(value)
    value=nil
  end
  tag='option'
  options[:with].merge!(:value => value.to_s) if value
  if options[:selected]
    options[:with].merge!(:selected => "selected")
  end
  options.delete(:selected)
  options.merge!(:text => text) if text
  @__current_scope_for_nokogiri_matcher.should have_tag(tag, options)
end

#with_password_field(name) ⇒ Object



331
332
333
334
# File 'lib/rspec2-rails-views-matchers.rb', line 331

def with_password_field name
  options = { :with => { :name => name, :type => 'password' } }
  should_have_input(options)
end

#with_radio_button(name, value) ⇒ Object



373
374
375
376
377
# File 'lib/rspec2-rails-views-matchers.rb', line 373

def with_radio_button name, value
  options = { :with => { :name => name, :type => 'radio' } }
  options[:with].merge!(:value => value)
  should_have_input(options)
end

#with_range_field(name, min, max, options = {}) ⇒ Object



301
302
303
304
# File 'lib/rspec2-rails-views-matchers.rb', line 301

def with_range_field name, min, max, options={}
  options = { :with => { :name => name, :type => 'range', :min => min.to_s, :max => max.to_s }.merge(options.delete(:with)||{}) }
  should_have_input(options)
end

#with_select(name, options = {}, &block) ⇒ Object



385
386
387
388
389
390
391
# File 'lib/rspec2-rails-views-matchers.rb', line 385

def with_select name, options={}, &block
  options[:with] ||= {}
  id = options[:with].delete(:id)
  tag='select'; tag += '#'+id if id
  options[:with].merge!(:name => name)
  @__current_scope_for_nokogiri_matcher.should have_tag(tag, options, &block)
end

#with_submit(value) ⇒ Object



455
456
457
458
# File 'lib/rspec2-rails-views-matchers.rb', line 455

def with_submit value
  options = { :with => { :type => 'submit', :value => value } }
  should_have_input(options)
end

#with_tag(tag, options = {}) { ... } ⇒ Object

Note:

this should be used within block of have_tag matcher

with_tag matcher

Yields:

See Also:



220
221
222
# File 'lib/rspec2-rails-views-matchers.rb', line 220

def with_tag tag, options={}, &block
  @__current_scope_for_nokogiri_matcher.should have_tag(tag, options, &block)
end

#with_text_area(name) ⇒ Object



351
352
353
354
# File 'lib/rspec2-rails-views-matchers.rb', line 351

def with_text_area name
  options = { :with => { :name => name } }
  @__current_scope_for_nokogiri_matcher.should have_tag('textarea', options)
end

#with_text_field(name, value = nil) ⇒ Object



253
254
255
256
257
# File 'lib/rspec2-rails-views-matchers.rb', line 253

def with_text_field name, value=nil
  options = { :with => { :name => name, :type => 'text' } }
  options[:with].merge!(:value => value) if value
  should_have_input(options)
end

#with_url_field(name, value = nil) ⇒ Object



277
278
279
280
281
# File 'lib/rspec2-rails-views-matchers.rb', line 277

def with_url_field name, value=nil
  options = { :with => { :name => name, :type => 'url' } }
  options[:with].merge!(:value => value) if value
  should_have_input(options)
end

#without_button(text, value = nil, options = {}) ⇒ Object



444
445
446
447
448
449
450
451
452
453
# File 'lib/rspec2-rails-views-matchers.rb', line 444

def without_button text, value=nil, options={}
  options[:with] ||= {}
  if value.is_a?(Hash)
    options.merge!(value)
    value=nil
  end
  options[:with].merge!(:value => value.to_s) if value
  options.merge!(:text => text) if text
  @__current_scope_for_nokogiri_matcher.should_not have_tag('button', options)
end

#without_checkbox(name, value = nil) ⇒ Object



367
368
369
370
371
# File 'lib/rspec2-rails-views-matchers.rb', line 367

def without_checkbox name, value=nil
  options = { :with => { :name => name, :type => 'checkbox' } }
  options[:with].merge!(:value => value) if value
  should_not_have_input(options)
end

#without_date_field(date_field_type, name = nil, options = {}) ⇒ Object



323
324
325
326
327
328
329
# File 'lib/rspec2-rails-views-matchers.rb', line 323

def without_date_field date_field_type, name=nil, options={}
  date_field_type = date_field_type.to_s
  raise "unknown type `#{date_field_type}` for date picker" unless DATE_FIELD_TYPES.include?(date_field_type)
  options = { :with => { :type => date_field_type.to_s }.merge(options.delete(:with)||{}) }
  options[:with].merge!(:name => name.to_s) if name
  should_not_have_input(options)
end

#without_email_field(name, value = nil) ⇒ Object



271
272
273
274
275
# File 'lib/rspec2-rails-views-matchers.rb', line 271

def without_email_field name, value=nil
  options = { :with => { :name => name, :type => 'email' } }
  options[:with].merge!(:value => value) if value
  should_not_have_input(options)
end

#without_file_field(name) ⇒ Object



346
347
348
349
# File 'lib/rspec2-rails-views-matchers.rb', line 346

def without_file_field name
  options = { :with => { :name => name, :type => 'file' } }
  should_not_have_input(options)
end

#without_hidden_field(name, value = nil) ⇒ Object



247
248
249
250
251
# File 'lib/rspec2-rails-views-matchers.rb', line 247

def without_hidden_field name, value=nil
  options = { :with => { :name => name, :type => 'hidden' } }
  options[:with].merge!(:value => value) if value
  should_not_have_input(options)
end

#without_number_field(name, value = nil) ⇒ Object



295
296
297
298
299
# File 'lib/rspec2-rails-views-matchers.rb', line 295

def without_number_field name, value=nil
  options = { :with => { :name => name, :type => 'number' } }
  options[:with].merge!(:value => value.to_s) if value
  should_not_have_input(options)
end

#without_option(text, value = nil, options = {}) ⇒ Object



417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
# File 'lib/rspec2-rails-views-matchers.rb', line 417

def without_option text, value=nil, options={}
  options[:with] ||= {}
  if value.is_a?(Hash)
    options.merge!(value)
    value=nil
  end
  tag='option'
  options[:with].merge!(:value => value.to_s) if value
  if options[:selected]
    options[:with].merge!(:selected => "selected")
  end
  options.delete(:selected)
  options.merge!(:text => text) if text
  @__current_scope_for_nokogiri_matcher.should_not have_tag(tag, options)
end

#without_password_field(name) ⇒ Object



336
337
338
339
# File 'lib/rspec2-rails-views-matchers.rb', line 336

def without_password_field name
  options = { :with => { :name => name, :type => 'password' } }
  should_not_have_input(options)
end

#without_radio_button(name, value) ⇒ Object



379
380
381
382
383
# File 'lib/rspec2-rails-views-matchers.rb', line 379

def without_radio_button name, value
  options = { :with => { :name => name, :type => 'radio' } }
  options[:with].merge!(:value => value)
  should_not_have_input(options)
end

#without_range_field(name, min = nil, max = nil, options = {}) ⇒ Object



306
307
308
309
310
311
# File 'lib/rspec2-rails-views-matchers.rb', line 306

def without_range_field name, min=nil, max=nil, options={}
  options = { :with => { :name => name, :type => 'range' }.merge(options.delete(:with)||{}) }
  options[:with].merge!(:min => min.to_s) if min
  options[:with].merge!(:max => max.to_s) if max
  should_not_have_input(options)
end

#without_select(name, options = {}, &block) ⇒ Object



393
394
395
396
397
398
399
# File 'lib/rspec2-rails-views-matchers.rb', line 393

def without_select name, options={}, &block
  options[:with] ||= {}
  id = options[:with].delete(:id)
  tag='select'; tag += '#'+id if id
  options[:with].merge!(:name => name)
  @__current_scope_for_nokogiri_matcher.should_not have_tag(tag, options, &block)
end

#without_submit(value) ⇒ Object



460
461
462
463
# File 'lib/rspec2-rails-views-matchers.rb', line 460

def without_submit value
  options = { :with => { :type => 'submit', :value => value } }
  should_not_have_input(options)
end

#without_tag(tag, options = {}) { ... } ⇒ Object

Note:

this should be used within block of have_tag matcher

without_tag matcher

Yields:

See Also:



228
229
230
# File 'lib/rspec2-rails-views-matchers.rb', line 228

def without_tag tag, options={}, &block
  @__current_scope_for_nokogiri_matcher.should_not have_tag(tag, options, &block)
end

#without_text_area(name) ⇒ Object



356
357
358
359
# File 'lib/rspec2-rails-views-matchers.rb', line 356

def without_text_area name
  options = { :with => { :name => name } }
  @__current_scope_for_nokogiri_matcher.should_not have_tag('textarea', options)
end

#without_text_field(name, value = nil) ⇒ Object



259
260
261
262
263
# File 'lib/rspec2-rails-views-matchers.rb', line 259

def without_text_field name, value=nil
  options = { :with => { :name => name, :type => 'text' } }
  options[:with].merge!(:value => value) if value
  should_not_have_input(options)
end

#without_url_field(name, value = nil) ⇒ Object



283
284
285
286
287
# File 'lib/rspec2-rails-views-matchers.rb', line 283

def without_url_field name, value=nil
  options = { :with => { :name => name, :type => 'url' } }
  options[:with].merge!(:value => value) if value
  should_not_have_input(options)
end