Module: Spec::Rails::Matchers
- Defined in:
- lib/spec/rails/matchers/views.rb,
lib/spec/rails/matchers/observers.rb,
lib/spec/rails/matchers/validations.rb,
lib/spec/rails/matchers/associations.rb
Defined Under Namespace
Classes: Observe
Instance Method Summary collapse
- #belong_to(association) ⇒ Object
- #have_and_belong_to_many(association) ⇒ Object
- #have_checkbox_for(attribute) ⇒ Object
- #have_form_posting_to(url_or_path) ⇒ Object
- #have_form_puting_to(url_or_path, id) ⇒ Object
- #have_label_for(attribute, text) ⇒ Object
- #have_link_to(url_or_path, text = nil) ⇒ Object
- #have_many(association) ⇒ Object
- #have_one(association) ⇒ Object
- #have_password_field_for(attribute) ⇒ Object
- #have_submit_button ⇒ Object
- #have_text_area_for(attribute) ⇒ Object
- #have_text_field_for(attribute) ⇒ Object
- #observe(expected_model_class) ⇒ Object
- #validate_confirmation_of(attribute) ⇒ Object
- #validate_length_of(attribute, options) ⇒ Object
- #validate_presence_of(attribute) ⇒ Object
- #validate_uniqueness_of(attribute) ⇒ Object
- #with_checkbox_for(attribute) ⇒ Object
- #with_label_for(attribute, text) ⇒ Object
- #with_link_to(url_or_path, text = nil) ⇒ Object
- #with_password_field_for(attribute) ⇒ Object
- #with_submit_button ⇒ Object
- #with_text_area_for(attribute) ⇒ Object
- #with_text_field_for(attribute) ⇒ Object
Instance Method Details
#belong_to(association) ⇒ Object
4 5 6 7 8 9 |
# File 'lib/spec/rails/matchers/associations.rb', line 4 def belong_to(association) return simple_matcher("model to belong to #{association}") do |model| model = model.class if model.is_a? ActiveRecord::Base model.reflect_on_all_associations(:belongs_to).find { |a| a.name == association } end end |
#have_and_belong_to_many(association) ⇒ Object
25 26 27 28 29 30 |
# File 'lib/spec/rails/matchers/associations.rb', line 25 def have_and_belong_to_many(association) return simple_matcher("model to have and belong to many #{association}") do |model| model = model.class if model.is_a? ActiveRecord::Base model.reflect_on_all_associations(:has_and_belongs_to_many).find { |a| a.name == association } end end |
#have_checkbox_for(attribute) ⇒ Object
65 66 67 68 69 |
# File 'lib/spec/rails/matchers/views.rb', line 65 def have_checkbox_for(attribute) return simple_matcher("have a checkbox for '#{attribute}'") do |response| have_tag("input##{attribute}[type=checkbox]").matches?(response) end end |
#have_form_posting_to(url_or_path) ⇒ Object
4 5 6 7 8 |
# File 'lib/spec/rails/matchers/views.rb', line 4 def have_form_posting_to(url_or_path) return simple_matcher("have a form submitting via POST to '#{url_or_path}'") do |response| have_tag("form[method=post][action=#{url_or_path}]").matches?(response) end end |
#have_form_puting_to(url_or_path, id) ⇒ Object
10 11 12 13 14 15 |
# File 'lib/spec/rails/matchers/views.rb', line 10 def have_form_puting_to(url_or_path, id) return simple_matcher("have a form submitting via PUT to '#{url_or_path}/#{id}'") do |response| have_tag("form[method=post][action=#{url_or_path}/#{id}]").matches?(response) have_tag("input[name=_method][type=hidden][value=put]").matches?(response) end end |
#have_label_for(attribute, text) ⇒ Object
17 18 19 20 21 |
# File 'lib/spec/rails/matchers/views.rb', line 17 def have_label_for(attribute, text) return simple_matcher("have a label for '#{attribute}' with value of '#{text}'") do |response| have_tag("label[for=#{attribute}]").matches?(response) end end |
#have_link_to(url_or_path, text = nil) ⇒ Object
89 90 91 92 93 |
# File 'lib/spec/rails/matchers/views.rb', line 89 def have_link_to(url_or_path, text = nil) return simple_matcher("have a link to '#{url_or_path}'") do |response| have_tag("a[href=#{url_or_path}]", text).matches?(response) end end |
#have_many(association) ⇒ Object
11 12 13 14 15 16 |
# File 'lib/spec/rails/matchers/associations.rb', line 11 def have_many(association) return simple_matcher("model to have many #{association}") do |model| model = model.class if model.is_a? ActiveRecord::Base model.reflect_on_all_associations(:has_many).find { |a| a.name == association } end end |
#have_one(association) ⇒ Object
18 19 20 21 22 23 |
# File 'lib/spec/rails/matchers/associations.rb', line 18 def have_one(association) return simple_matcher("model to have one #{association}") do |model| model = model.class if model.is_a? ActiveRecord::Base model.reflect_on_all_associations(:has_one).find { |a| a.name == association } end end |
#have_password_field_for(attribute) ⇒ Object
53 54 55 56 57 |
# File 'lib/spec/rails/matchers/views.rb', line 53 def have_password_field_for(attribute) return simple_matcher("have a password field for '#{attribute}'") do |response| have_tag("input##{attribute}[type=password]").matches?(response) end end |
#have_submit_button ⇒ Object
77 78 79 80 81 |
# File 'lib/spec/rails/matchers/views.rb', line 77 def return simple_matcher("have a submit button") do |response| have_tag("input[type=submit]").matches?(response) end end |
#have_text_area_for(attribute) ⇒ Object
41 42 43 44 45 |
# File 'lib/spec/rails/matchers/views.rb', line 41 def have_text_area_for(attribute) return simple_matcher("have a text field for '#{attribute}'") do |response| have_tag("textarea##{attribute}[type=text]").matches?(response) end end |
#have_text_field_for(attribute) ⇒ Object
29 30 31 32 33 |
# File 'lib/spec/rails/matchers/views.rb', line 29 def have_text_field_for(attribute) return simple_matcher("have a text field for '#{attribute}'") do |response| have_tag("input##{attribute}[type=text]").matches?(response) end end |
#observe(expected_model_class) ⇒ Object
28 29 30 |
# File 'lib/spec/rails/matchers/observers.rb', line 28 def observe(expected_model_class) Observe.new(expected_model_class) end |
#validate_confirmation_of(attribute) ⇒ Object
48 49 50 51 52 53 |
# File 'lib/spec/rails/matchers/validations.rb', line 48 def validate_confirmation_of(attribute) return simple_matcher("model to validate the confirmation of #{attribute}") do |model| model.send("#{attribute}_confirmation=", 'asdf') !model.valid? && model.errors.invalid?(attribute) end end |
#validate_length_of(attribute, options) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/spec/rails/matchers/validations.rb', line 11 def validate_length_of(attribute, ) if .has_key? :within min = [:within].first max = [:within].last elsif .has_key? :is min = [:is] max = min elsif .has_key? :minimum min = [:minimum] elsif .has_key? :maximum max = [:maximum] end return simple_matcher("model to validate the length of #{attribute} within #{min || 0} and #{max || 'Infinity'}") do |model| invalid = false if !min.nil? && min >= 1 model.send("#{attribute}=", 'a' * (min - 1)) invalid = !model.valid? && model.errors.invalid?(attribute) end if !max.nil? model.send("#{attribute}=", 'a' * (max + 1)) invalid ||= !model.valid? && model.errors.invalid?(attribute) end invalid end end |
#validate_presence_of(attribute) ⇒ Object
4 5 6 7 8 9 |
# File 'lib/spec/rails/matchers/validations.rb', line 4 def validate_presence_of(attribute) return simple_matcher("model to validate the presence of #{attribute}") do |model| model.send("#{attribute}=", nil) !model.valid? && model.errors.invalid?(attribute) end end |
#validate_uniqueness_of(attribute) ⇒ Object
41 42 43 44 45 46 |
# File 'lib/spec/rails/matchers/validations.rb', line 41 def validate_uniqueness_of(attribute) return simple_matcher("model to validate the uniqueness of #{attribute}") do |model| model.class.stub!(:find).and_return(true) !model.valid? && model.errors.invalid?(attribute) end end |
#with_checkbox_for(attribute) ⇒ Object
71 72 73 74 75 |
# File 'lib/spec/rails/matchers/views.rb', line 71 def with_checkbox_for(attribute) return simple_matcher("have a checkbox for '#{attribute}'") do |response| with_tag("input##{attribute}[type=checkbox]").matches?(response) end end |
#with_label_for(attribute, text) ⇒ Object
23 24 25 26 27 |
# File 'lib/spec/rails/matchers/views.rb', line 23 def with_label_for(attribute, text) return simple_matcher("have a label for '#{attribute}' with value of '#{text}'") do |response| with_tag("label[for=#{attribute}]").matches?(response) end end |
#with_link_to(url_or_path, text = nil) ⇒ Object
95 96 97 98 99 |
# File 'lib/spec/rails/matchers/views.rb', line 95 def with_link_to(url_or_path, text = nil) return simple_matcher("have a link to '#{url_or_path}'") do |response| with_tag("a[href=#{url_or_path}]", text).matches?(response) end end |
#with_password_field_for(attribute) ⇒ Object
59 60 61 62 63 |
# File 'lib/spec/rails/matchers/views.rb', line 59 def with_password_field_for(attribute) return simple_matcher("have a password field for '#{attribute}'") do |response| with_tag("input##{attribute}[type=password]").matches?(response) end end |
#with_submit_button ⇒ Object
83 84 85 86 87 |
# File 'lib/spec/rails/matchers/views.rb', line 83 def return simple_matcher("have a submit button") do |response| with_tag("input[type=submit]").matches?(response) end end |
#with_text_area_for(attribute) ⇒ Object
47 48 49 50 51 |
# File 'lib/spec/rails/matchers/views.rb', line 47 def with_text_area_for(attribute) return simple_matcher("have a text field for '#{attribute}'") do |response| with_tag("textarea##{attribute}[type=text]").matches?(response) end end |
#with_text_field_for(attribute) ⇒ Object
35 36 37 38 39 |
# File 'lib/spec/rails/matchers/views.rb', line 35 def with_text_field_for(attribute) return simple_matcher("with a text field for '#{attribute}'") do |response| with_tag("input##{attribute}[type=text]").matches?(response) end end |