· 7 years ago · Jul 07, 2018, 02:34 PM
1## test_helper.rb
2ENV["RAILS_ENV"] = "test"
3require File.expand_path(File.dirname(__FILE__) + "/../config/environment")
4require 'test_help'
5require 'authlogic/test_case'
6
7class ActiveSupport::TestCase
8 self.use_transactional_fixtures = true
9 self.use_instantiated_fixtures = false
10
11 def assert_delivers_email(options, &block)
12 ActionMailer::Base.deliveries.clear
13 yield
14 assert_not_nil email = ActionMailer::Base.deliveries[0]
15 assert email.to.include?(options[:to]) if options[:to]
16 assert_equal options[:from], email.from if options[:from]
17 assert_equal options[:subject], email.subject if options[:subject]
18 assert_match /#{options[:body]}/i, email.body if options[:body]
19 end
20
21 def self.should_have_before_filters(*filters)
22 should "have before filters for #{filters.join(',')}" do
23 chain = controller.class.filter_chain.select {|filter| filter.class == ActionController::Filters::BeforeFilter}
24 methods = chain.map{|filter| filter.method}
25 filters.each{|filter| assert methods.include?(filter)}
26 end
27 end
28
29 def self.should_protect_from_forgery
30 should_have_before_filters :verify_authenticity_token
31 should "set the authenticity token" do
32 assert_equal :authenticity_token, controller.class.request_forgery_protection_token
33 end
34 end
35
36 def self.should_filter_parameter_logging(options)
37 keys = options[:for].map{|k| k.to_s}
38 keys.each {|key|
39 should "filter #{key} parameter values from the log" do
40 begin
41 assert_equal '[FILTERED]', controller.send(:filter_parameters, key => 'secret')[key]
42 rescue
43 flunk "Could not filter parameters"
44 end
45 end
46 }
47 end
48
49 def self.should_require_user(&block)
50 should "redirect to the login if no user is logged in" do
51 puts "1"
52 yield
53 puts "3"
54 assert_response :redirect
55 assert_redirected_to login_url
56 assert_not_nil session[:return_to]
57 end
58
59 should "not redirect to the login if a user is logged in" do
60 Session.create(Factory(:confirmed_user))
61 yield
62 assert_response :success
63 end
64 end
65
66 def self.should_not_require_user(&block)
67 should "not require a user to be logged in" do
68 yield
69 assert_response :success
70 end
71 end
72
73end
74
75
76
77## test
78
79require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
80
81class SessionsControllerTest < ActionController::TestCase
82 setup :activate_authlogic
83 context "Sessions" do
84 should_require_user { post :destroy }
85 should_not_require_user { post :create }
86 should_not_require_user { get :new }
87 end
88end
89
90
91## Error
92
93 3) Error:
94test: Sessions should not redirect to the login if a user is logged in. (SessionsControllerTest):
95NoMethodError: undefined method `post' for SessionsControllerTest:Class
96 /opt/local/lib/ruby/gems/1.8/gems/thoughtbot-shoulda-2.10.1/lib/shoulda/context.rb:300:in `send'
97 /opt/local/lib/ruby/gems/1.8/gems/thoughtbot-shoulda-2.10.1/lib/shoulda/context.rb:300:in `method_missing'
98 /test/functional/sessions_controller_test.rb:6:in `__bind_1247705431_56142'
99 test/test_helper.rb:58:in `__bind_1247705431_422529'
100 /opt/local/lib/ruby/gems/1.8/gems/thoughtbot-shoulda-2.10.1/lib/shoulda/context.rb:253:in `call'
101 /opt/local/lib/ruby/gems/1.8/gems/thoughtbot-shoulda-2.10.1/lib/shoulda/context.rb:253:in `test: Sessions should not redirect to the login if a user is logged in. '
102
103 4) Error:
104test: Sessions should not require a user to be logged in. (SessionsControllerTest):
105NoMethodError: undefined method `get' for SessionsControllerTest:Class
106 /opt/local/lib/ruby/gems/1.8/gems/thoughtbot-shoulda-2.10.1/lib/shoulda/context.rb:300:in `send'
107 /opt/local/lib/ruby/gems/1.8/gems/thoughtbot-shoulda-2.10.1/lib/shoulda/context.rb:300:in `method_missing'
108 /test/functional/sessions_controller_test.rb:8:in `__bind_1247705431_56142'
109 test/test_helper.rb:65:in `__bind_1247705431_466719'
110 /opt/local/lib/ruby/gems/1.8/gems/thoughtbot-shoulda-2.10.1/lib/shoulda/context.rb:253:in `call'
111 /opt/local/lib/ruby/gems/1.8/gems/thoughtbot-shoulda-2.10.1/lib/shoulda/context.rb:253:in `test: Sessions should not require a user to be logged in. '
112
113 5) Error:
114test: Sessions should redirect to the login if no user is logged in. (SessionsControllerTest):
115NoMethodError: undefined method `post' for SessionsControllerTest:Class
116 /opt/local/lib/ruby/gems/1.8/gems/thoughtbot-shoulda-2.10.1/lib/shoulda/context.rb:300:in `send'
117 /opt/local/lib/ruby/gems/1.8/gems/thoughtbot-shoulda-2.10.1/lib/shoulda/context.rb:300:in `method_missing'
118 /test/functional/sessions_controller_test.rb:6:in `__bind_1247705431_56142'
119 test/test_helper.rb:50:in `__bind_1247705431_470247'
120 /opt/local/lib/ruby/gems/1.8/gems/thoughtbot-shoulda-2.10.1/lib/shoulda/context.rb:253:in `call'
121 /opt/local/lib/ruby/gems/1.8/gems/thoughtbot-shoulda-2.10.1/lib/shoulda/context.rb:253:in `test: Sessions should redirect to the login if no user is logged in. '