0

我正在指定我的 VideoController 创建操作:

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 回答