ruby - Why doesn't Minitest's assert_raises work as expected in this case? -


i'm trying sort out weird behavior in rails using action controller bug report template.

for record, controller in template:

class testcontroller < actioncontroller::base   include rails.application.routes.url_helpers    def index     render text: 'home'   end end 

i've added route missing action:

routes.draw   '/' => 'test#index'   '/missing' => 'test#missing' end 

and i'm trying assert abstractcontroller::actionnotfound raised when following route:

class bugtest < minitest::test   include rack::test::methods    def test_missing     assert_raises(abstractcontroller::actionnotfound) { '/missing' }   end    private     def app       rails.application     end end 

expected behavior: green test.

actual behavior:

# running tests:  d, [2014-04-24t09:17:41.948985 #4497] debug -- : d, [2014-04-24t09:17:41.949029 #4497] debug -- : i, [2014-04-24t09:17:41.949094 #4497]  info -- : started "/missing" 127.0.0.1 @ 2014-04-24 09:17:41 +0200 f, [2014-04-24t09:17:41.951337 #4497] fatal -- : abstractcontroller::actionnotfound (the action 'missing' not found testcontroller): [stacktrace]    1) failure: bugtest#test_missing [action_controller_gem.rb:45]: abstractcontroller::actionnotfound expected nothing raised. 

so, basically, the exception raised minitest claiming nothing raised. stumped here.

why can't minitest assert abstractcontroller::actionnotfound raised? i've looked rack::test::methods rule out get works threads or couldn't find anything.

i've looked @ implementation of assert_raises -- nothing obvious.

why doesn't assert_raises(abstractcontroller::actionnotfound) { '/missing' } pass?

(never mind fact tested in rails. i'm trying past real deal.)

this isn't problem assert_raises, continues work fine. issue having exception raising within rails app getting handled rails app , not propagating out test. calling get '/missing' raising abstractcontroller::actionnotfound error, app handles error , returns appropriate response (404 or 500) client (your test).

okay, how go testing controller did raise error expecting? use actioncontroller::testcase test test controllers. but, when call action on controller doesn't in actioncontroller::testcase different error. following:

require "test_helper"  class testcontrollertest < actioncontroller::testcase    def test_missing     assert_raises abstractcontroller::actionnotfound       :missing     end   end end 

produces following output:

  1) failure: testcontrollertest#test_missing [test/controllers/test_controller_test.rb:6]: [abstractcontroller::actionnotfound] exception expected, not class: <actioncontroller::urlgenerationerror> message: <"no route matches {:action=>\"missing\", :controller=>\"test\"}"> ---backtrace--- test/controllers/test_controller_test.rb:7:in `block in test_missing' test/controllers/test_controller_test.rb:6:in `test_missing' --------------- 

the reason because actioncontroller::testcase aware of routes , won't allow calling action not in routes. trying test. and, assume why aren't using actioncontroller::testcase.

at point wonder if aren't testing shouldn't. perhaps should allow rails job , trust behaves correctly. hey, we've come far, why not go way , test rails tests anyway. instead of making call through full rails app, let's try calling testcontroller directly. if create new instance of controller can ask handle action if action not defined on controller. have dive deep how actioncontroller works , make use of abstractcontroller#process method:

require "test_helper"  class testcontrollertest < minitest::test   def test_missing     assert_raises abstractcontroller::actionnotfound       testcontroller.new.process :missing     end   end end 

now asking controller process action doesn't exist , testing controller behaves expected. yay?


Comments

Popular posts from this blog

java - Intellij Synchronizing output directories .. -

git - Initial Commit: "fatal: could not create leading directories of ..." -