我正在为 cotroller 编写测试,但没有成功。我的测试是:
describe VideosController do
describe 'index' do
it 'should select the index template for rendering' do
Video.stub(:video_exists?).with("KgfdlZuVz7I").and_return(true)
get :index, { :q => "KgfdlZuVz7I" }
response.should render_template('index')
end
end
end
这里是控制器。
class VideosController < ApplicationController
def index
if params[:q]
params_hash = CGI::parse(params[:q])
if Video.video_exists?(params_hash.values[0][0])
video = Video.new :video_id => params_hash.values[0][0]
if video.save!
flash[:notification] = "Video found."
else
flash[:notification] = "Video found but not saved to database."
end
redirect_to root_path
else
flash[:notification] = "Video not found."
redirect_to root_path
end
end
end
end
测试未通过并引发消息:
VideosController 索引应选择用于渲染失败/错误的索引模板:get :index, { :q => "KgfdlZuVz7I" } received :video_exists? 预期有未预期的参数:(“KgfdlZuVz7I”)得到:(无参数)如果消息也可能与其他参数一起接收,请先存根默认值。# ./app/controllers/videos_controller.rb:5:in
index' # ./spec/controllers/videos_controller_spec.rb:12:in
block (3 levels) in '
认为我没有以正确的方式存根视频,因为我只存根 video_exists?但不是新的和保存。但我不知道如何解决这个问题,因为我是 TDD 和 Rspec 的新手。