2

出于某种原因,我的规范没有通过。似乎@categories没有达到规范。

这是控制器:

class CategoriesController < ApplicationController
  def index
    @categories = Category.all
  end
end

我的规格:

require 'spec_helper'

describe CategoriesController do

  describe "GET #index" do
    category = FactoryGirl.create(:category)
    subject { get :index }
    it { @categories.should include category }
  end

end

错误:

Failure/Error: @categories.should include category
     NoMethodError:
       undefined method `include?' for nil:NilClass
4

2 回答 2

3

不,但您可以使用assigns规范助手访问它们:

require 'spec_helper'

describe CategoriesController do

  describe "GET #index" do
    category = FactoryGirl.create(:category)
    subject { get :index }
    it { assigns(:categories).should include category }
  end

end
于 2012-06-09T10:27:55.470 回答
0

实例变量是在控制器而不是规范中设置的。

如果您想确保在控制器中设置实例变量,那么您应该使用

assigns(:categories).should include category

更多信息请点击这里:

https://www.relishapp.com/rspec/rspec-rails/v/2-10/docs/controller-specs

于 2012-06-09T10:32:12.553 回答