应该:测试控制器过滤器
所以我决定调查 Rails 的测试状况。 在阅读了一些关于 Rails 测试文档如何糟糕的接受答案后,我开始同意。 到目前为止,我显然不属于理解 Rails 测试的人——尽管我非常了解这个框架,至少我是这么认为的。
我读到 Shoulda 是一个很好用的插件,所以我正在使用它。 我的第一个测试:“确保方法需要 ID 参数”...嗯,确定这是有道理的; 我想确保我的控制器上的 before_filter :validate_id 确实有效。 这是测试我的索引 GET 是否按预期方式工作所需的 3 步中的第 1 步。 失败! 显然,测试环境的运行方式与正常的 Rails 堆栈不同:
...
lib/action_controller/base.rb:524:in `process_without_filters'
...
在 ActionController::TestCase 使用 get {} 时检查我能找到的所有地方,实际上没有包含过滤器的信息。 我在哪里加入“Rails 测试者俱乐部”才能解决这个问题?
context "GET :index requires id" do
setup {
get :index
}
should_redirect_to('/') { }
end
So I have decided to investigate the state of Rails' testing. After reading a few accepted answers here on SO talking about how Rails' testing documentation is atrocious I am starting to agree. Thus far, I am apparently not in the club of people that understand Rails testing- even though I understand the framework quite well, or so I thought.
I've read that Shoulda is a good plugin to use so I'm using it. My first test: "ensure method requires an ID parameter"... um sure this makes sense; I want to make sure that my before_filter :validate_id on my controller actually works. This is step 1 of the 3 required to test that my index's GET works the way it is supposed to. FAIL! Clearly the test environment runs differently than the normal Rails stack:
...
lib/action_controller/base.rb:524:in `process_without_filters'
...
checking everywhere I can results in practically no information on including filters when ActionController::TestCase uses get {}. Where do I join "the club of Rails testers" so I can figure this out?
context "GET :index requires id" do
setup {
get :index
}
should_redirect_to('/') { }
end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您确定失败与 before_filters 有关吗? 我认为不会。 进一步查找堆栈中的其他错误...我可以通过登录要求测试我的应用程序,没有问题。
您断言索引需要一个 id...但请求它时没有 id。 是不是因为没有id而报错? 也许您需要抓住这一点,或者使用assert_raises 测试失败模式。
Are you sure that failure has anything to do with before_filters? I don't think it does. Look further up the stack for other errors... I'm able to test my app with login requirements no problem.
You're asserting that index requires an id...but requesting it without one. Is it throwing an error because there is no id? Maybe you need to catch that, or test the failure mode with assert_raises.
我同意安德鲁的观点,问题可能出在其他地方。 我有大量的功能测试可以与带有 before_filters 的控制器一起使用,并且从未遇到过您指定的问题。
I agree with Andrew that the problem probably lies elsewhere. I have plenty of functional tests that work with controllers with before_filters, and have never run into the problem you specify.