VideoController create アクションを指定しています:
it 'creates a new video given valid parameters' do
video = { :title=>"Example title", :description=>"Description", :url=>"http://www.youtube.com/watch?v=b6speA_XhP4", :provider=>"Youtube" }
Video.should_receive(:save).with("title"=>"Example title", :description=>"Description", :provider=>"Youtube", :views=>0, :likes=>0, :provider_video_id=>'b6speA_XhP4', :thumb=>"http://img.youtube.com/vi/b6speA_XhP4/2.jpg")
post :create, :video => video
end
エラーが発生します:
1) VideosController POST create creates a new video given valid parameters
Failure/Error: Video.should_receive(:save).with("title"=>"Example title", :description=>"Description", :provider=>"Youtube", :views=>0, :likes=>0, :provider_video_id=>'b6speA_XhP4', :thumb=>"http://img.youtube.com/vi/b6speA_XhP4/2.jpg")
(<Video(id: integer, title: string, description: text, thumb: string, provider_video_id: string, provider: string, views: integer, likes: integer, created_at: datetime, updated_at: datetime) (class)>).save({"title"=>"Example title", :description=>"Description", :provider=>"Youtube", :views=>0, :likes=>0, :provider_video_id=>"b6speA_XhP4", :thumb=>"http://img.youtube.com/vi/b6speA_XhP4/2.jpg"})
expected: 1 time
received: 0 times
ただし、出力は、RSpec の期待値と結果の両方が同じであることを示しています。
# extracted from the output
# EXPECTATION :
"title"=>"Example title", :description=>"Description", :provider=>"Youtube", :views=>0, :likes=>0, :provider_video_id=>'b6speA_XhP4', :thumb=>"http://img.youtube.com/vi/b6speA_XhP4/2.jpg"
# RESULT :
"title"=>"Example title", :description=>"Description", :provider=>"Youtube", :views=>0, :likes=>0, :provider_video_id=>"b6speA_XhP4", :thumb=>"http://img.youtube.com/vi/b6speA_XhP4/2.jpg"
ビデオコントローラー
def create
method = 'get_' + params[:video][:provider] + '_video_id'
params[:video][:provider_video_id] = Video.send(method, params[:video][:url])
params[:video][:thumb] = Video.get_thumb_from_youtube(params[:video][:provider_video_id])
params[:video][:views] = params[:video][:likes] = 0
@video = Video.new(params[:video])
if @video.save!
redirect_to video_path(@video), notice:'Video added successfully.'
else
render :new
end
end