【发布时间】:2014-04-24 08:47:14
【问题描述】:
我正在尝试使用 Action Controller bug report template 来解决 Rails 中的一个奇怪行为。
作为记录,这是模板中的控制器:
class TestController < ActionController::Base
include Rails.application.routes.url_helpers
def index
render text: 'Home'
end
end
我已经添加了一个缺少动作的路由:
routes.draw do
get '/' => 'test#index'
get '/missing' => 'test#missing'
end
我试图断言AbstractController::ActionNotFound 在遵循该路线时引发:
class BugTest < Minitest::Test
include Rack::Test::Methods
def test_missing
assert_raises(AbstractController::ActionNotFound) { get '/missing' }
end
private
def app
Rails.application
end
end
预期行为:绿色测试。
实际行为:
# 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 GET "/missing" for 127.0.0.1 at 2014-04-24 09:17:41 +0200
F, [2014-04-24T09:17:41.951337 #4497] FATAL -- :
AbstractController::ActionNotFound (The action 'missing' could not be found for TestController):
[stacktrace]
1) Failure:
BugTest#test_missing [action_controller_gem.rb:45]:
AbstractController::ActionNotFound expected but nothing was raised.
所以,基本上,引发了异常,但 Minitest 声称没有引发任何事情。我在这里被难住了。
为什么 Minitest 不能断言 AbstractController::ActionNotFound 被提出?我查看了 Rack::Test::Methods 以排除 get 与线程或其他东西一起使用但找不到任何东西。
我也看过implementation of assert_raises——没什么明显的。
为什么assert_raises(AbstractController::ActionNotFound) { get '/missing' } 不通过?
(别介意这已经在 Rails 中测试过了。我正在努力克服这个问题。)
【问题讨论】:
-
请显示
TestController。和完整的BugTest类 -
为清楚起见添加了它。我没有修改
TestController的模板(在第一句中链接),只是将test_missing添加到BugTest。 -
你
TestController应该继承自ApplicationController。或者你有完整的app 示例? -
我拿了我们现在都引用的AC模板,添加了一个路由并添加了一个测试方法。为什么要改
TestController的超类? -
我的意思是你有完整的列表或块?因为我用这个,这个对我有用。
标签: ruby unit-testing ruby-on-rails-4 minitest