Rcov: Why is this code not being considered covered?
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 =&g开发者_如何转开发t; @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?
My guess is you are using ruby 1.9? Rcov does not support ruby 1.9 and produces impredictable results. Use SimpleCov instead.
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.
context "MyController index" do
should "respond with success" do
get :index
assert_response :success
end
end
精彩评论