Module: Etest::Assertions
- Included in:
- MiniTest::Unit::TestCase
- Defined in:
- lib/etest/assertions.rb
Overview
Some assertions
Instance Method Summary collapse
- #assert_equal(expected, actual) ⇒ Object
- #assert_file_doesnt_exist(*paths) ⇒ Object
- #assert_file_exist(*paths) ⇒ Object
-
#assert_invalid(model, *attributes) ⇒ Object
Verifies that a model is invalid.
- #assert_invalid_xml(*args) ⇒ Object
-
#assert_matches(actual, pattern) ⇒ Object
regex matching.
- #assert_nil(actual) ⇒ Object
- #assert_no_match(actual, pattern) ⇒ Object
- #assert_not_equal(unexpected, actual) ⇒ Object
- #assert_not_nil(actual) ⇒ Object
- #assert_nothing_raised(msg = nil, &block) ⇒ Object
- #assert_raise(klass, msg = nil, &block) ⇒ Object
- #assert_raises_kind_of(klass, msg = nil, &block) ⇒ Object
- #assert_respond_to(obj, *args) ⇒ Object
- #assert_route(uri_path, params) ⇒ Object
-
#assert_valid(model, *attributes) ⇒ Object
Verifies that a model is valid.
- #assert_valid_xml(*args) ⇒ Object
- #catch_exception_on(&block) ⇒ Object
-
#invalid_attributes(model) ⇒ Object
returns a list of invalid attributes in a model, as symbols.
- #libxml_installed? ⇒ Boolean
- #xml_valid?(xml) ⇒ Boolean
Instance Method Details
#assert_equal(expected, actual) ⇒ Object
22 23 24 |
# File 'lib/etest/assertions.rb', line 22 def assert_equal(expected, actual) assert expected == actual, "Expected\n\n\t#{expected.inspect}\n\ninstead of\n\n\t#{actual.inspect}" end |
#assert_file_doesnt_exist(*paths) ⇒ Object
150 151 152 153 154 |
# File 'lib/etest/assertions.rb', line 150 def assert_file_doesnt_exist(*paths) paths.flatten.each do |path| assert !File.exist?(path), "File should not exist: #{path}" end end |
#assert_file_exist(*paths) ⇒ Object
144 145 146 147 148 |
# File 'lib/etest/assertions.rb', line 144 def assert_file_exist(*paths) paths.flatten.each do |path| assert File.exist?(path), "Missing file: #{path}" end end |
#assert_invalid(model, *attributes) ⇒ Object
Verifies that a model is invalid. Pass in some attributes to only validate those attributes.
55 56 57 58 59 60 61 62 63 64 |
# File 'lib/etest/assertions.rb', line 55 def assert_invalid(model, *attributes) assert(!model.valid?, "#{model.inspect} should be invalid, but isn't.") return if attributes.empty? missing_invalids = attributes - invalid_attributes(model) assert missing_invalids.empty?, "Attribute(s) #{missing_invalids.join(", ")} should be invalid, but are not" end |
#assert_invalid_xml(*args) ⇒ Object
77 78 79 80 81 82 83 84 |
# File 'lib/etest/assertions.rb', line 77 def assert_invalid_xml(*args) return unless libxml_installed? args.push @response.body if args.empty? args.each do |xml| assert !xml_valid?(xml), "XML should not be valid: #{xml}" end end |
#assert_matches(actual, pattern) ⇒ Object
regex matching
8 9 10 |
# File 'lib/etest/assertions.rb', line 8 def assert_matches(actual, pattern) assert actual =~ pattern, "#{actual.truncate(32).inspect} should match #{pattern.inspect}, but doesn't." end |
#assert_nil(actual) ⇒ Object
164 165 166 |
# File 'lib/etest/assertions.rb', line 164 def assert_nil(actual) assert actual.nil?, "#{actual.inspect} should be nil" end |
#assert_no_match(actual, pattern) ⇒ Object
12 13 14 15 |
# File 'lib/etest/assertions.rb', line 12 def assert_no_match(actual, pattern) return unless actual =~ pattern assert actual !~ pattern, "#{actual.truncate(32).inspect} should not match #{pattern.inspect}, but does." end |
#assert_not_equal(unexpected, actual) ⇒ Object
18 19 20 |
# File 'lib/etest/assertions.rb', line 18 def assert_not_equal(unexpected, actual) assert unexpected != actual, "#{actual} equals #{unexpected}, when it shouldn't" end |
#assert_not_nil(actual) ⇒ Object
140 141 142 |
# File 'lib/etest/assertions.rb', line 140 def assert_not_nil(v) assert !v.nil?, "Should be nil, but is an #{v.class.name} object" end |
#assert_nothing_raised(msg = nil, &block) ⇒ Object
126 127 128 129 |
# File 'lib/etest/assertions.rb', line 126 def assert_nothing_raised(msg=nil, &block) ex = catch_exception_on(&block) assert ex.nil?, msg || "Should not raise an exception, but raised a #{ex.class.name} exception" end |
#assert_raise(klass, msg = nil, &block) ⇒ Object
117 118 119 120 121 122 123 124 |
# File 'lib/etest/assertions.rb', line 117 def assert_raise(klass, msg=nil, &block) ex = catch_exception_on(&block) if ex.nil? assert false, msg || "Should raise a #{klass} exception, but didn't raise at all" else assert ex.class.name == klass.name, msg || "Should raise a #{klass} exception, but raised a #{ex.class.name} exception" end end |
#assert_raises_kind_of(klass, msg = nil, &block) ⇒ Object
131 132 133 134 135 136 137 138 |
# File 'lib/etest/assertions.rb', line 131 def assert_raises_kind_of(klass, msg=nil, &block) ex = catch_exception_on(&block) if ex.nil? assert false, msg || "Should raise a #{klass} exception, but didn't raise at all" else assert ex.is_a?(klass), msg || "Should raise a #{klass} exception, but raised a #{ex.class.name} exception" end end |
#assert_respond_to(obj, *args) ⇒ Object
26 27 28 29 30 31 32 |
# File 'lib/etest/assertions.rb', line 26 def assert_respond_to(obj, *args) raise ArgumentError, "Missing argument(s)" if args.length < 1 args.reject! { |sym| obj.respond_to?(sym) } assert args.empty?, "#{obj.inspect} should respond to #{args.map(&:inspect).join(", ")}, but doesn't." end |
#assert_route(uri_path, params) ⇒ Object
106 107 108 |
# File 'lib/etest/assertions.rb', line 106 def assert_route(uri_path, params) assert_recognizes params, uri_path end |
#assert_valid(model, *attributes) ⇒ Object
Verifies that a model is valid. Pass in some attributes to only validate those attributes.
42 43 44 45 46 47 48 49 50 |
# File 'lib/etest/assertions.rb', line 42 def assert_valid(model, *attributes) if attributes.empty? assert(model.valid?, "#{model.inspect} should be valid, but isn't: #{model.errors..join(", ")}.") else invalid_attributes = invalid_attributes(model) & attributes assert invalid_attributes.empty?, "Attribute(s) #{invalid_attributes.join(", ")} should be valid" end end |
#assert_valid_xml(*args) ⇒ Object
68 69 70 71 72 73 74 75 |
# File 'lib/etest/assertions.rb', line 68 def assert_valid_xml(*args) return unless libxml_installed? args.push @response.body if args.empty? args.each do |xml| assert xml_valid?(xml), "XML is not valid: #{xml}" end end |
#catch_exception_on(&block) ⇒ Object
110 111 112 113 114 115 |
# File 'lib/etest/assertions.rb', line 110 def catch_exception_on(&block) yield nil rescue Exception $! end |
#invalid_attributes(model) ⇒ Object
returns a list of invalid attributes in a model, as symbols.
35 36 37 |
# File 'lib/etest/assertions.rb', line 35 def invalid_attributes(model) #:nodoc: model.valid? ? [] : model.errors.instance_variable_get("@errors").keys.map(&:to_sym) end |
#libxml_installed? ⇒ Boolean
86 87 88 89 90 91 92 93 94 95 |
# File 'lib/etest/assertions.rb', line 86 def libxml_installed? return @libxml_installed unless @libxml_installed.nil? @libxml_installed = begin require "libxml" true rescue LoadError STDERR.puts "*** Skipping xml_validation. Please install the 'libxml' gem" false end end |
#xml_valid?(xml) ⇒ Boolean
97 98 99 100 101 102 103 104 |
# File 'lib/etest/assertions.rb', line 97 def xml_valid?(xml) LibXML::XML::Error.reset_handler LibXML::XML::Document.io StringIO.new(xml) true rescue LibXML::XML::Error false end |