2015-10-06 60 views
0

你好,我有一個Rspec Test來做一個用戶控制器索引操作。對於用戶身份驗證,我自己編碼並且不使用任何寶石。ActionView :: Template :: Error:未定義的方法'children'for nil:NilClas

修整運行測試時出現此錯誤。我查了所有的解決方案,沒有任何工作。

2) UsersController GET #index redirects visitor 
    Failure/Error: user_logged_in 
    ActionView::Template::Error: 
     undefined method `children' for nil:NilClass 

這是測試。

require "rails_helper" 

RSpec.describe UsersController, type: :controller do 
    let!(:user) { create(:user) } 

    let!(:users) do 
    users = [] 
    3.times { users << create(:user) } 
    users 
    end 

    describe "GET #index" do 
    before do 
     user_logged_in 
     create(users) 
    end 

    it "user renders template and shows users" do 
     get :index 
     expect(response).to render_template(:index) 
     expect(response).to have_http_status(:success) 
     expect(assigns(:users)).to eq(users) 
    end 

    it "redirects visitor" do 
     get :index 
     it { expect(response).to redirect_to(root_path) } 
    end 
    end 
end 

這裏是rails_helper

ENV['RAILS_ENV'] ||= 'test' 
require File.expand_path('../../config/environment', __FILE__) 

abort("The Rails environment is running in production mode!") if Rails.env.production? 
require 'spec_helper' 
require 'rspec/rails' 
require "shoulda/matchers" 

ActiveRecord::Migration.maintain_test_schema! 

RSpec.configure do |config| 
    config.fixture_path = "#{::Rails.root}/spec/fixtures" 
    config.include FactoryGirl::Syntax::Methods 
    config.use_transactional_fixtures = true 
    config.infer_spec_type_from_file_location! 
    config.include Capybara::DSL 
end 

def admin_logged_in 
    visit login_path 
    fill_in 'Email', with: admin.email 
    fill_in 'Password', with: admin.password 
    click_button 'Log In' 
end 

def user_logged_in 
    visit login_path 
    fill_in 'Email', with: user.email 
    fill_in 'Password', with: user.password 
    click_button 'Log In' 
end 
+0

請顯示你的'index'方法和完整的錯誤堆棧跟蹤。 –

回答

0

沒有看到完整的代碼是很難判斷。 我想到的一件事是,我認爲你會濫用it {}語法。你爲什麼不把它改寫爲

it "redirects visitor" do 
    get :index 
    expect(response).to redirect_to(root_path) 
end 
相關問題