2012-04-06 23 views
1

写测试情况下,我不能让CURRENT_USER我必须写集成测试的情况下对我的一个功能列表页面和功能指标法有一个像下面为什么同时使用RSpec和水豚

def index 
    @food_categories = current_user.food_categories 
end 

现在,当我尝试代码写测试用例为此,它抛出一个错误

'undefined method features for nil class' because it can not get the current user 

现在我有什么做的是低于 我每个语句之前写在登录过程,然后编写测试用例

上的功能列表页面

您能否让我知道我如何获得current_user

仅供参考,我已经用色器件宝石和集成测试工作的情况下使用RSpec

这里是my spec file 这里是我的food_categories_spec.rb

+0

嘿@ chirag.sweng你可能要交换周围的链接名称你提供。 spec_helper文件指向foot_categories_spec文件,反之亦然。真的只是一个音符。 这里是我的spec文件这里是我的food_categories_spec.rb – 2014-06-09 11:16:23

回答

3

更新:你混淆功能测试和集成测试。集成测试不使用get,因为没有控制器要测试,而必须使用visit(某些url)。然后你必须检查页面的内容,而不是响应代码(后者用于功能测试)。它可能看起来像:

visit '/food_categories' 
page.should have_content 'Eggs' 
page.should have_content 'Fats and oils' 

如果你需要功能测试,这里有一个例子:

# spec/controllers/your_controller_spec.rb 
describe YourController do 

    before do 
    @user = FactoryGirl.create(:user) 
    sign_in @user 
    end 

    describe "GET index" do 

    before do 
     get :index 
    end 

    it "is successful" do 
     response.should be_success 
    end 

    it "assings user features" do 
     assigns(:features).should == @user.features 
    end 
    end 
end 

# spec/spec_helper.rb 
RSpec.configure do |config| 
    #... 
    config.include Devise::TestHelpers, :type => :controller 
end 
+0

谢谢,但现在它抛出错误未定义的方法'sign_in'为#,请让我知道如果您有任何想法 – 2012-04-06 09:13:12

+0

仅供参考我正在编写集成测试案例 – 2012-04-06 09:19:44

+0

@ chirag.sweng查看更新回答 – 2012-04-06 09:36:31