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))


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

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'


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

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)



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

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



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

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



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

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


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

def redirect(*args)
  if args.empty?
    test_case.assert_response @object.response.redirected_to, :redirect
  elsif args.length == 1 and args.first.is_a?(String)
    test_case.assert_equal args.first, @object.response.redirected_to
  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 @object.response.redirected_to.starts_with?('https:')
    elsif secure == false
      test_case.assert @object.response.redirected_to.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)


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

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



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

def validate
  test_case.assert_valid @object
end