0

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
4

1 に答える 1

0

あなたがパラメータを渡している方法は私には奇妙に見えます、あなたの場合

it 'creates a new video given valid parameters' do
  params = {:video => { :title=>"Example title", :description=>"Description", :url=>"http://www.youtube.com/watch?v=b6speA_XhP4", :provider=>"Youtube" }}
  Video.should_receive(:save).with(params[:video])
  post :create, params
end

パラメータを受け入れる方法はハッシュのハッシュであり、仕様に欠けているように見えます。

于 2012-06-30T04:49:15.210 に答える