Module: Etest::Assertions

Included in:
MiniTest::Unit::TestCase
Defined in:
lib/etest/assertions.rb

Overview

Some assertions

Defined Under Namespace

Modules: Etest

Instance Method Summary collapse

Instance Method Details

#assert_file_doesnt_exist(*paths) ⇒ Object



133
134
135
136
137
# File 'lib/etest/assertions.rb', line 133

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



127
128
129
130
131
# File 'lib/etest/assertions.rb', line 127

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.



38
39
40
41
42
43
44
45
46
47
# File 'lib/etest/assertions.rb', line 38

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



60
61
62
63
64
65
66
67
# File 'lib/etest/assertions.rb', line 60

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_nil(actual) ⇒ Object



147
148
149
# File 'lib/etest/assertions.rb', line 147

def assert_nil(actual)
  assert actual.nil?, "#{actual.inspect} should be nil"
end

#assert_not_equal(unexpected, actual) ⇒ Object



5
6
7
# File 'lib/etest/assertions.rb', line 5

def assert_not_equal(unexpected, actual)
  assert unexpected != actual, "#{actual} equals #{unexpected}, when it shouldn't"
end

#assert_not_nil(actual) ⇒ Object



123
124
125
# File 'lib/etest/assertions.rb', line 123

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



109
110
111
112
# File 'lib/etest/assertions.rb', line 109

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



100
101
102
103
104
105
106
107
# File 'lib/etest/assertions.rb', line 100

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



114
115
116
117
118
119
120
121
# File 'lib/etest/assertions.rb', line 114

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

Raises:

  • (ArgumentError)


9
10
11
12
13
14
15
# File 'lib/etest/assertions.rb', line 9

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



89
90
91
# File 'lib/etest/assertions.rb', line 89

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.



25
26
27
28
29
30
31
32
33
# File 'lib/etest/assertions.rb', line 25

def assert_valid(model, *attributes)
  if attributes.empty?
    assert(model.valid?, "#{model.inspect} should be valid, but isn't: #{model.errors.full_messages.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



51
52
53
54
55
56
57
58
# File 'lib/etest/assertions.rb', line 51

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



93
94
95
96
97
98
# File 'lib/etest/assertions.rb', line 93

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.



18
19
20
# File 'lib/etest/assertions.rb', line 18

def invalid_attributes(model)                                     #:nodoc:
  model.valid? ? [] : model.errors.instance_variable_get("@errors").keys.map(&:to_sym)
end

#libxml_installed?Boolean

Returns:

  • (Boolean)


69
70
71
72
73
74
75
76
77
78
# File 'lib/etest/assertions.rb', line 69

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

Returns:

  • (Boolean)


80
81
82
83
84
85
86
87
# File 'lib/etest/assertions.rb', line 80

def xml_valid?(xml)
  LibXML::XML::Error.reset_handler
  
  LibXML::XML::Document.io StringIO.new(xml)
  true
rescue LibXML::XML::Error
  false
end