- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
我正在尝试使用 Action Controller bug report template 解决 Rails 中的一个奇怪行为.
为了记录,这是模板中的 Controller :
class TestController < ActionController::Base
include Rails.application.routes.url_helpers
def index
render text: 'Home'
end
end
我已经为缺失的 Action 添加了一条路线:
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 中测试过的事实。我正在努力克服这个问题以获得真正的交易。)
最佳答案
这不是 assert_raises
的问题,它继续正常工作。您遇到的问题是您在 Rails 应用程序中引发的异常正在由您的 Rails 应用程序处理,而不是传播到您的测试。调用 get '/missing'
会引发 AbstractController::ActionNotFound
错误,但随后您的应用会处理错误并向客户端返回适当的响应(404 或 500)(你的测试)。
好的,那么您将如何测试您的 Controller 是否确实引发了您预期的错误?通常,您只需使用 ActionController::TestCase
测试来测试您的 Controller 。但是,当您在不在 ActionController::TestCase
中的 Controller 上调用操作时,您会得到一个不同的错误。所以如下:
require "test_helper"
class TestControllerTest < ActionController::TestCase
def test_missing
assert_raises AbstractController::ActionNotFound do
get :missing
end
end
end
产生以下输出:
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'
---------------
原因是因为 ActionController::TestCase
知道路由并且不允许调用不在路由中的操作。但这正是您要测试的。而且,我假设您为什么不使用 ActionController::TestCase
。
在这一点上,我想知道您是否没有测试一些您不应该做的事情。也许您应该允许 Rails 完成它的工作并相信它的行为是正确的。但是,嘿,我们已经走到这一步了,为什么不一路走下去,测试一下 Rails 测试的内容。让我们尝试直接调用 TestController
,而不是通过完整的 Rails 应用程序进行调用。如果我们创建 Controller 的一个新实例,我们可以要求它处理一个 Action ,即使该 Action 没有在 Controller 上定义。但要做到这一点,我们将不得不深入研究 ActionController 的工作原理并利用 AbstractController#process
方法:
require "test_helper"
class TestControllerTest < Minitest::Test
def test_missing
assert_raises AbstractController::ActionNotFound do
TestController.new.process :missing
end
end
end
现在我们要求 Controller 处理一个不存在的 Action ,并测试 Controller 的行为是否符合预期。好吗?
关于ruby - 为什么 Minitest 的 assert_raises 在这种情况下没有按预期工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23264064/
我对java有点陌生,所以如果我犯了一个简单的错误,请原谅我,但我不确定我哪里出错了,我收到的错误是“预期的.class,预期的标识符,而不是声明, ';'预期的。”我尝试了不同的方法,并从这些方法中
This question already has answers here: chai test array equality doesn't work as expected (3个答案) 3年前
我正在学习 Java(对不起,我的英语很差,这不是我的母语),当我在 Eclipse (JavaSE-1.7) 中在我输入的每个“try”中执行“try-finally” block 时,会出现以下消
我收到两个错误,指出 token 上的语法错误,ConstructorHeaderName expected instead & token “(”上的语法错误,< expected 在线: mTM.
我找不到错误。 Eclipse 给我这个错误。每个 { } 都是匹配的。请帮忙。 Multiple markers at this line - Syntax error on token “)”,
代码: import java.awt.*; import javax.swing.*; import java.awt.event.*; public class DoubleIt extends
我正在用 python(Vs 代码)编写代码,但出现此错误: Expected ")" Pylance 错误发生在:def main() 我试着运行我的 main 并将它打印到我的屏幕上。我用谷歌搜
我正在尝试按照 documentation 中的建议使用异步函数。但我收到此错误 意外的 token ,预期 ( async function getMoviesFromApi() { try
Closed. This question does not meet Stack Overflow guidelines。它当前不接受答案。 想改善这个问题吗?更新问题,以便将其作为on-topic
Closed. This question does not meet Stack Overflow guidelines。它当前不接受答案。 想改善这个问题吗?更新问题,以便将其作为on-topic
第一行包含一个表示数组长度的整数p。第二行包含用空格分隔的整数,这些整数描述数组中的每个元素。第三行打印一个整数,指示负数组的数量。 package asgn3; import java.util.*
好的,我是初学者,我必须修复此 java 表达式语言代码才能在我的系统 (Windchill) 中工作,但看起来我在语法中遗漏了一些内容: LWCNormalizedObject lwc =
我无法编译我的程序! 我想我缺少一个花括号,但我怎么也看不出在哪里! import javax.swing.*; import java.awt.*;
我的 jQuery 代码有问题,我的 Firebug 向我发出警告:需要选择器。 这是代码: $("img[id$='_tick']").each(function() { $(this).c
我的新类(class) Fountainofyouth 遇到了问题。尝试构建整个项目后,调试器显示 warning: extended initializer lists only available
我已经从 Java 转向 CPP,并且正在努力围绕构造构造函数链进行思考,我认为这是我的问题的根源。 我的头文件如下: public: GuidedTour(); GuidedTour(string
鉴于以下 for(var i=0; i< data.cats.length; i++) list += buildCategories(data.cats[i]); jsLint 告诉我 Expect
我有这个 json,但 Visual Studio Code 在标题中给了我警告。 [ { "title": "Book A", "imageUrl": "https:
我正在尝试编写一个有条件地禁用四个特殊成员函数(复制构造、移动构造、复制赋值和移动赋值)的包装类,下面是我用于测试目的的快速草稿: enum class special_member : uint8_
所以我用 F# 编写了一个非常简单的程序,它应该对 1000 以下的所有 3 和 5 的倍数求和: [1..999] |> List.filter (fun x -> x % 3 = 0 || x %
我是一名优秀的程序员,十分优秀!