2

我是 TDD on Rails 的新手,我正在尝试使用“使用 Rails 进行日常 Rails 测试”一书来测试 videos_controller 以获得简单的显示页面,但它给了我错误“nil”并呈现为空,如下所示。但我真的不知道我做错了什么。请给我提意见。谢谢您的帮助。

规格/控制器/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)>
4

1 回答 1

2

有一个before_filterin 控制器

before_filter :require_user

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

要修复,只需添加必要的操作,比如创建用户或登录,之前get :show

于 2013-05-13T02:35:40.737 回答