Rcov:为什么这段代码不被视为覆盖?
这是我的控制器:
class MyController < ApplicationController
include MyHelper
def index
get_list_from_params do |list|
@list = list
respond_to do |format|
format.html
format.xml { render :xml => @list }
format.json { render :json => @list }
end
end
end
end
...它所基于的助手:
module MyHelper
def get_list_from_params(param = :id, &on_success)
raw_id = params[param]
begin
id = Integer(raw_id)
rescue
render :template => "invalid_id", :locals => {:id => raw_id }
else
yield MyList.new(id)
end
end
end
...以及我的功能测试(使用 Shoulda):
class MyControllerTest < ActionController::TestCase
context "MyController index" do
setup do
get :index
end
should_respond_with :success
end
end
编辑 我的 rcov rake 与官方列出的完全相同常见问题解答:eigenclass.org
RCov (0.9.7.1) 列出了控制器中的每一行将“def index”设置为绿色,之后的每一行(包括所有“end”)设置为红色/未执行。我知道当我的测试实际执行时,它确实成功执行了代码。
为什么 RCov 给出的结果不直观?我在这里缺少什么吗?
Here's my controller:
class MyController < ApplicationController
include MyHelper
def index
get_list_from_params do |list|
@list = list
respond_to do |format|
format.html
format.xml { render :xml => @list }
format.json { render :json => @list }
end
end
end
end
...the helper that it's based on:
module MyHelper
def get_list_from_params(param = :id, &on_success)
raw_id = params[param]
begin
id = Integer(raw_id)
rescue
render :template => "invalid_id", :locals => {:id => raw_id }
else
yield MyList.new(id)
end
end
end
...and my functional test (which is using Shoulda):
class MyControllerTest < ActionController::TestCase
context "MyController index" do
setup do
get :index
end
should_respond_with :success
end
end
EDIT My rcov rake is exactly the same as the one listed in the official FAQ: eigenclass.org
RCov (0.9.7.1) lists every line in the controller up to "def index" as green, and every line after that (including all of the "end"s) as red/unexecuted. I know that when my test actually executes, it does execute the code successfully.
Why does RCov give unintuitive results? Is there something I'm missing here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我猜你使用的是 ruby 1.9? Rcov 不支持 ruby 1.9 并且会产生不可预测的结果。使用 SimpleCov 代替。
My guess is you are using ruby 1.9? Rcov does not support ruby 1.9 and produces impredictable results. Use SimpleCov instead.
我的猜测是 rcov 仅认为实际测试用例中运行的代码被测试“覆盖”。从技术上讲,您对“获取索引”的调用不在测试用例中,而是在设置块中。应该有一些有趣的设置范围问题,也许 rcov 不够聪明,无法意识到这一点。
尝试将代码放入测试用例块中(见下文)——只是为了看看这是否会改变事情。注意:我认为您不必像这样运行测试 - 只是看看它是否有效。
My guess is that rcov only considers code run in actual test-cases as "covered" by a test. your call to "get index" is not technically in a test-case, but in the setup block. shoulda has interesting scope-issues with setup and perhaps rcov simply isn't smart enough to realise that.
Try putting the code into a test case block (see below) - just to see if that changes things. Note: I don't think you should have to run your tests like this - it's just to see if it works.