Question

I'm new to TDD on Rails, and I'm trying to test videos_controller with the book "Everyday Rails Testing with Rails" for simple show page but it gives me error 'nil' & render empty, as follows. But I really don't know what I'm doing wrong. Please advise me. Thank you for your help.

spec/controllers/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

app/controllers/videos_controller.rb

class VideosController < ApplicationController
  before_filter :require_user  

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

Error I got in my terminal:

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)>
Was it helpful?

Solution

There is a before_filter in controller

before_filter :require_user

You have not supplied the required action in the test, so the real action show has not been reached yet.

To fix, just add necessary action say creating user or login, before get :show

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top