控制器测试使用Rspec显示页面,它给出错误nil&并呈现空白页面

时间:2013-05-13 00:21:26

标签: ruby-on-rails unit-testing rspec tdd

我是Rails上的TDD新手,我正在尝试使用“Everyday Rails Testing with Rails”一书来测试videos_controller,以获得简单的显示页面,但它给了我错误'nil'和amp;渲染为空,如下。但我真的不知道我做错了什么。请建议我。谢谢你的帮助。

规格/控制器/ videos_controller_spec.rb

require 'spec_helper'

describe VideosController do
  describe "GET show" do
   before {@video1 = Video.create!(title: 'Family Guy', description: 'Some description')}

     it "assigns the requested video to the @video" do
      get :show, id: @video1
      assigns(:video).should == @video1
    end

    it "render show tempalte" do
      get :show, id: @video1
      response.should render_template :show
    end
  end
end

应用程序/控制器/ videos_controller.rb

class VideosController < ApplicationController
  before_filter :require_user  

  def show 
    @video = Video.find(params[:id])
  end
end

我的终端出错了:

Failures:

  1) VideosController GET show assigns the requested video to the @video
     Failure/Error: assigns(:video).should == @video1
   expected: #<Video id: 1, title: "Family Guy", small_cover_url: nil, large_cover_url: nil, description: "Some description", created_at: "2013-05-13 00:12:32", updated_at: "2013-05-13 00:12:32">
        got: nil (using ==)
 # ./spec/controllers/videos_controller_spec.rb:9:in `block (3 levels) in <top (required)>'

  2) VideosController GET show render show tempalte
     Failure/Error: response.should render_template :show
   expecting <"show"> but rendering with <"">
 # ./spec/controllers/videos_controller_spec.rb:14:in `block (3 levels) in <top (required)>

1 个答案:

答案 0 :(得分:2)

控制器中有before_filter

before_filter :require_user

您尚未在测试中提供所需的操作,因此尚未达到实际操作show

要修复,只需在get :show

之前添加必要的操作,例如创建用户或登录