Module: Test::Spec::Rails::ShouldExpectations

Defined in:
lib/test/spec/rails/expectations.rb

Instance Method Summary collapse

Instance Method Details

#cache_pages(*pages, &block) ⇒ Object

Tests whether certain pages are cached.

lambda { get :index }.should.cache_pages(posts_path)
lambda { get :show, :id => post }.should.cache_pages(post_path(post), formatted_posts_path(:js, post))


90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/test/spec/rails/expectations.rb', line 90

def cache_pages(*pages, &block)
  if block
    block.call
  else
    @object.call
  end
  cache_dir = ActionController::Base.page_cache_directory
  files = Dir.glob("#{cache_dir}/**/*").map do |filename|
    filename[cache_dir.length..-1]
  end
  test_case.assert pages.all? { |page| files.include?(page) }
end

#differ(*expected) ⇒ Object Also known as: change

Tests whether the evaluation of the expression changes.

lambda { Norm.create }.should.differ('Norm.count')
lambda { Norm.create; Norm.create }.should.differ('Norm.count', +2)
lambda { Norm.create; Option.create }.should.differ('Norm.count', +2, 'Option.count', +1)

norm = lambda { Norm.create }.should.differ('Norm.count')
norm.name.should == 'Latency'


68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/test/spec/rails/expectations.rb', line 68

def differ(*expected)
  block_binding = @object.send(:binding)
  before = expected.in_groups_of(2).map do |expression, _|
    eval(expression, block_binding)
  end
  
  block_result = @object.call
  
  expected.in_groups_of(2).each_with_index do |(expression, difference), index|
    difference = 1 if difference.nil?
    error = "#{expression.inspect} didn't change by #{difference}"
    test_case.assert_equal(before[index] + difference, eval(expression, block_binding), error)
  end
  
  block_result
end

#dom_equal(expected) ⇒ Object

Test two HTML strings for equivalency (e.g., identical up to reordering of attributes)



104
105
106
# File 'lib/test/spec/rails/expectations.rb', line 104

def dom_equal(expected)
  test_case.assert_dom_equal expected, @object
end

#equal_list(expected) ⇒ Object

Tests if the array of records is the same, order must be the same



119
120
121
122
123
124
125
126
# File 'lib/test/spec/rails/expectations.rb', line 119

def equal_list(expected)
  message = "#{Helpers.inspect_records(@object)} does not have the same records as #{Helpers.inspect_records(expected)}"
  
  left = @object.map(&:id)
  right = expected.map(&:id)
  
  test_case.assert(left == right, message)
end

#equal_set(expected) ⇒ Object

Tests if the array of records is the same, order may vary



109
110
111
112
113
114
115
116
# File 'lib/test/spec/rails/expectations.rb', line 109

def equal_set(expected)
  message = "#{Helpers.inspect_records(@object)} does not have the same records as #{Helpers.inspect_records(expected)}"
  
  left = @object.map(&:id).sort
  right = expected.map(&:id).sort
  
  test_case.assert(left == right, message)
end

#redirect(*args) ⇒ Object Also known as: redirect_to

Test that we were redirected somewhere:

should.redirect

Test that we were redirected to a specific url:

should.redirect :controller => 'foo', :action => 'bar'

or:

should.redirect_to :controller => 'foo', :action => 'bar', :secure => true


16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/test/spec/rails/expectations.rb', line 16

def redirect(*args)
  redirect_url = @object.response.respond_to?(:redirect_url) ? @object.response.redirect_url : @object.response.redirected_to
  if args.empty?
    test_case.assert_response redirect_url, :redirect
  elsif args.length == 1 and args.first.is_a?(String)
    test_case.assert_equal args.first, redirect_url
  else
    options = args.extract_options!
    if secure = options.delete(:secure)
      unless secure == true or secure == false
        raise ArgumentError, ":secure option should be a boolean"
      end
    end
    
    @object.instance_eval { test_case.assert_redirected_to *args }
    if secure == true
      test_case.assert redirect_url.starts_with?('https:')
    elsif secure == false
      test_case.assert redirect_url.starts_with?('http:')
    end
  end
end

#redirect_back_to(url_options) ⇒ Object

Tests whether a redirect back to the HTTP_REFERER was send.

lambda { delete :destroy, :id => 1 }.should.redirect_back_to(articles_url)
lambda { delete :destroy, :id => 1 }.should.redirect_back_to(:action => :index)


44
45
46
47
48
49
50
51
52
53
# File 'lib/test/spec/rails/expectations.rb', line 44

def redirect_back_to(url_options)
  test_case = eval("self", @object.binding)
  url = test_case.controller.url_for(url_options)
  test_case.controller.request.env["HTTP_REFERER"] = url
  
  block_result = @object.call
  test_case.should.redirect_to(url)
  
  block_result
end

#validateObject

Test that the object is valid



56
57
58
# File 'lib/test/spec/rails/expectations.rb', line 56

def validate
  test_case.assert_valid @object
end

#validate_with(attribute, value) ⇒ Object

Tests if the model has errors on the attribute after validation for the presented value



129
130
131
132
133
134
135
136
137
138
# File 'lib/test/spec/rails/expectations.rb', line 129

def validate_with(attribute, value)
  message = "Expected #{attribute.inspect} with value `#{value.inspect}' to validate"
  @object.send("#{attribute}=", value)
  @object.valid?
  if @object.errors[attribute].kind_of?(Array)
    test_case.assert(@object.errors[attribute].empty?, message)
  else
    test_case.assert(@object.errors.on(attribute).nil?, message)
  end
end