Module: RSpec::Matchers

Defined in:
lib/rspec-html-matchers.rb

Constant Summary

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

Instance Method Summary (collapse)

Instance Method Details

- (Object) have_form(action_url, method, options = {}) { ... }

form assertion

it is a shortcut to

have_tag 'form', :with => { :action => action_url, :method => method ... }

Yields:

  • block with with_<field>, see below

See Also:



292
293
294
295
296
297
298
299
# File 'lib/rspec-html-matchers.rb', line 292

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

- (Object) have_tag(tag, options = {}) { ... }

tag assertion, this is the core of functionality, other matchers are shortcuts to this 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)         # shortcut for 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, e.g. 'div', 'section#my', 'article.red'

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

    options hash(see below)

Options Hash (options):

  • :with (Hash)

    hash with html attributes, within this, :class option 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)

    for tag count matching(ATTENTION: do not use :count with :minimum(:min) or :maximum(:max))

  • :count (Range)

    not strict tag count matching, count of tags should be in specified range

  • :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

  • :text (String/Regexp)

    to match tag content, could be either String or Regexp

Yields:

  • block where you should put with_tag, without_tag and/or other matchers



249
250
251
252
253
# File 'lib/rspec-html-matchers.rb', line 249

def have_tag tag, options={}, &block
  # for backwards compatibility with rpecs have tag:
  options = { :text => options } if options.kind_of? String
  @__current_scope_for_nokogiri_matcher = NokogiriMatcher.new(tag, options, &block)
end

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



483
484
485
486
487
488
489
490
491
492
# File 'lib/rspec-html-matchers.rb', line 483

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

- (Object) with_checkbox(name, value = nil)



415
416
417
418
# File 'lib/rspec-html-matchers.rb', line 415

def with_checkbox name, value=nil
  options = form_tag_options('checkbox',name,value)
  should_have_input(options)
end

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



367
368
369
370
371
372
373
# File 'lib/rspec-html-matchers.rb', line 367

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

- (Object) with_email_field(name, value = nil)



323
324
325
326
# File 'lib/rspec-html-matchers.rb', line 323

def with_email_field name, value=nil
  options = form_tag_options('email',name,value)
  should_have_input(options)
end

- (Object) with_file_field(name, value = nil)



393
394
395
396
# File 'lib/rspec-html-matchers.rb', line 393

def with_file_field name, value=nil
  options = form_tag_options('file',name,value)
  should_have_input(options)
end

- (Object) with_hidden_field(name, value = nil)

TODO fix code duplications



303
304
305
306
# File 'lib/rspec-html-matchers.rb', line 303

def with_hidden_field name, value=nil
  options = form_tag_options('hidden',name,value)
  should_have_input(options)
end

- (Object) with_number_field(name, value = nil)



343
344
345
346
# File 'lib/rspec-html-matchers.rb', line 343

def with_number_field name, value=nil
  options = form_tag_options('number',name,value)
  should_have_input(options)
end

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



451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
# File 'lib/rspec-html-matchers.rb', line 451

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

- (Object) with_password_field(name, value = nil)



383
384
385
386
# File 'lib/rspec-html-matchers.rb', line 383

def with_password_field name, value=nil
  options = form_tag_options('password',name,value)
  should_have_input(options)
end

- (Object) with_radio_button(name, value)



425
426
427
428
# File 'lib/rspec-html-matchers.rb', line 425

def with_radio_button name, value
  options = form_tag_options('radio',name,value)
  should_have_input(options)
end

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



353
354
355
356
# File 'lib/rspec-html-matchers.rb', line 353

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

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



435
436
437
438
439
440
441
# File 'lib/rspec-html-matchers.rb', line 435

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

- (Object) with_submit(value)



505
506
507
508
509
# File 'lib/rspec-html-matchers.rb', line 505

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

- (Object) with_tag(tag, options = {}) { ... }

Note:

this should be used within block of have_tag matcher

with_tag matcher

Yields:

  • block where you should put other with_tag or without_tag

See Also:



274
275
276
# File 'lib/rspec-html-matchers.rb', line 274

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

- (Object) with_text(text)

Raises:

  • (StandardError)


255
256
257
258
259
260
# File 'lib/rspec-html-matchers.rb', line 255

def with_text text
  raise StandardError, 'this matcher should be used inside "have_tag" matcher block' unless defined?(@__current_scope_for_nokogiri_matcher)
  raise ArgumentError, 'this matcher does not accept block' if block_given?
  tag = @__current_scope_for_nokogiri_matcher.instance_variable_get(:@tag)
  @__current_scope_for_nokogiri_matcher.should have_tag(tag, :text => text)
end

- (Object) with_text_area(name)

TODO, text=nil



403
404
405
406
407
# File 'lib/rspec-html-matchers.rb', line 403

def with_text_area name#TODO, text=nil
  #options = form_tag_options('text',name,value)
  options = { :with => { :name => name } }
  @__current_scope_for_nokogiri_matcher.should have_tag('textarea', options)
end

- (Object) with_text_field(name, value = nil)



313
314
315
316
# File 'lib/rspec-html-matchers.rb', line 313

def with_text_field name, value=nil
  options = form_tag_options('text',name,value)
  should_have_input(options)
end

- (Object) with_url_field(name, value = nil)



333
334
335
336
# File 'lib/rspec-html-matchers.rb', line 333

def with_url_field name, value=nil
  options = form_tag_options('url',name,value)
  should_have_input(options)
end

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



494
495
496
497
498
499
500
501
502
503
# File 'lib/rspec-html-matchers.rb', line 494

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

- (Object) without_checkbox(name, value = nil)



420
421
422
423
# File 'lib/rspec-html-matchers.rb', line 420

def without_checkbox name, value=nil
  options = form_tag_options('checkbox',name,value)
  should_not_have_input(options)
end

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



375
376
377
378
379
380
381
# File 'lib/rspec-html-matchers.rb', line 375

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

- (Object) without_email_field(name, value = nil)



328
329
330
331
# File 'lib/rspec-html-matchers.rb', line 328

def without_email_field name, value=nil
  options = form_tag_options('email',name,value)
  should_not_have_input(options)
end

- (Object) without_file_field(name, value = nil)



398
399
400
401
# File 'lib/rspec-html-matchers.rb', line 398

def without_file_field name, value=nil
  options = form_tag_options('file',name,value)
  should_not_have_input(options)
end

- (Object) without_hidden_field(name, value = nil)



308
309
310
311
# File 'lib/rspec-html-matchers.rb', line 308

def without_hidden_field name, value=nil
  options = form_tag_options('hidden',name,value)
  should_not_have_input(options)
end

- (Object) without_number_field(name, value = nil)



348
349
350
351
# File 'lib/rspec-html-matchers.rb', line 348

def without_number_field name, value=nil
  options = form_tag_options('number',name,value)
  should_not_have_input(options)
end

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



467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
# File 'lib/rspec-html-matchers.rb', line 467

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

- (Object) without_password_field(name, value = nil)



388
389
390
391
# File 'lib/rspec-html-matchers.rb', line 388

def without_password_field name, value=nil
  options = form_tag_options('password',name,value)
  should_not_have_input(options)
end

- (Object) without_radio_button(name, value)



430
431
432
433
# File 'lib/rspec-html-matchers.rb', line 430

def without_radio_button name, value
  options = form_tag_options('radio',name,value)
  should_not_have_input(options)
end

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



358
359
360
361
362
363
# File 'lib/rspec-html-matchers.rb', line 358

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

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



443
444
445
446
447
448
449
# File 'lib/rspec-html-matchers.rb', line 443

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

- (Object) without_submit(value)



511
512
513
514
515
# File 'lib/rspec-html-matchers.rb', line 511

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

- (Object) without_tag(tag, options = {}) { ... }

Note:

this should be used within block of have_tag matcher

without_tag matcher

Yields:

  • block where you should put other with_tag or without_tag

See Also:



282
283
284
# File 'lib/rspec-html-matchers.rb', line 282

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

- (Object) without_text(text) Also known as: but_without_text

Raises:

  • (StandardError)


262
263
264
265
266
267
# File 'lib/rspec-html-matchers.rb', line 262

def without_text text
  raise StandardError, 'this matcher should be used inside "have_tag" matcher block' unless defined?(@__current_scope_for_nokogiri_matcher)
  raise ArgumentError, 'this matcher does not accept block' if block_given?
  tag = @__current_scope_for_nokogiri_matcher.instance_variable_get(:@tag)
  @__current_scope_for_nokogiri_matcher.should_not have_tag(tag, :text => text)
end

- (Object) without_text_area(name)

TODO, text=nil



409
410
411
412
413
# File 'lib/rspec-html-matchers.rb', line 409

def without_text_area name#TODO, text=nil
  #options = form_tag_options('text',name,value)
  options = { :with => { :name => name } }
  @__current_scope_for_nokogiri_matcher.should_not have_tag('textarea', options)
end

- (Object) without_text_field(name, value = nil)



318
319
320
321
# File 'lib/rspec-html-matchers.rb', line 318

def without_text_field name, value=nil
  options = form_tag_options('text',name,value)
  should_not_have_input(options)
end

- (Object) without_url_field(name, value = nil)



338
339
340
341
# File 'lib/rspec-html-matchers.rb', line 338

def without_url_field name, value=nil
  options = form_tag_options('url',name,value)
  should_not_have_input(options)
end