您好,我有一个Rspec测试即可为用户控制器索引操作做。对于用户身份验证,我自己编码,而不是使用任何宝石。
在修改运行测试时出现此错误。我查找了所有解决方案,没有任何效果。
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 :(得分:0)
没有看到完整的代码,很难判断。
我想到的一件事是,我认为你滥用it {}
语法。为什么不把它重写为
it "redirects visitor" do
get :index
expect(response).to redirect_to(root_path)
end