I am writing test for cotroller but without succes. My test is:
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
Here is controller.
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
Test doesn’t pass and it raises message:
VideosController index should select the index template for rendering
Failure/Error: get :index, { :q => “KgfdlZuVz7I” }
received :video_exists? with une
xpected arguments
expected: (“KgfdlZuVz7I”)
got: (no args)
Please stub a default value first if message might be received with other args as well.
# ./app/controllers/videos_controller.rb:5:inindex'block (3 levels) in ‘
# ./spec/controllers/videos_controller_spec.rb:12:in
Think I am not stubbing Video on right way, because I stubbed just video_exists? but not new and save. But I don’t know how to resolve this since I am new in TDD and Rspec.
As piece of advice #1 you should not write out your “random string” twice like you did in your test. That is extremely susceptible to mistyping and very difficult to visually verify. Use this instead:
Also, I’m not sure what
params_hash = CGI::parse(params[:q])is supposed to be doing. Why not just use the params like normal?