RSpec controller spec passes without corresponding instance variable in controller

I wrote a spec to test the instance variable @vendors in the index action of my vendors controller. If I remove @vendors from the vendors controller the spec still passes. Any ideas as to why assigns(:vendors) would pass if @vendors doesn't exists in the controller. Heres my code:

Vendors Controller

class VendorsController < ApplicationController
  load_and_authorize_resource

  def index
    # @vendors = Vendor.all
  end

end  

Vendors Controller Spec

require 'spec_helper'
require 'ruby-debug'

describe VendorsController do
  login_user

  before(:each) do
    @vendor = Factory(:vendor)
  end

  describe "GET index" do
    before(:each) do
      @ability.can :read, Vendor
    end
    it "assigns all vendors to @vendors" do
      get :index
      assigns(:vendors).should == [@vendor]
    end
    it "should render the index template" do
      get :index
      response.should be_success
      response.code.should eq("200")
      response.should render_template("index")
    end
  end
end

Vendors Factory

FactoryGirl.define do
  factory :vendor do |f|
    f.sequence(:name) { |n| "Test#{n}" }
    f.sequence(:address) { |n| "000 Test#{n} drive, Hampton" }
    f.state "Virginia"
    f.zip "00000"
    f.sequence(:telephone) { |n| "000-000-000#{n}" }
    f.sequence(:poc) { |n| "Test#{n}" }
  end
end

Thanks

Answers


Because load_and_authorize_resource actually loads and authorizes.

So your code is unnecessary.

You could change with authorize_resource, thus the spec will fail.


Need Your Help

Modally Presenting a Navigation View Controller in a UITabBarController With a Segmented Control

ios uinavigationcontroller uitabbarcontroller modalviewcontroller

I've spent the past few days searching on the web for a solution to my problem, however, I can't seem to find a problem similar to mine. I am using a TabBarController and on one of the tabs I have a

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.