私はActionView::Template::Error: undefined method [] for nil:NilClass
がよくあるエラーだと知っていますが、私の問題に答えるものはありません。私はログインし、新しいプロジェクトページに行き、フォームに記入して新しいプロジェクトを作成する、非常に簡単なテストを持っています。しかし、ログインしてレールがロードされると、Rspec/Capybaraのダッシュボードはページを正しく読み取れず、フォームをテストする前にページが失敗します。Rspec&Capybara:ActionView :: Template :: Error:未定義メソッド `'for nil:NilClass
Rspec/Capybaraは、ログインページのデバイスフォームに問題はありませんが、どのクラスプロジェクトを認識していないようで、そのNilClassを考えると思われません。
ここでspecファイルされる:
require 'rails_helper'
describe "Creating a new project" do
let!(:client) { Client.create(name: "abc", code: "abc") }
let!(:project) { Project.create(name: "abc", client: client) }
let!(:user) { User.create(email: "[email protected]", password: "123123123") }
# Sign In with user
before :each do
visit root_url
fill_in 'user[email]', with: '[email protected]'
fill_in 'user[password]', with: '123123123'
find('input[name="commit"]').click
end
it "saves new project and returns to list of projects on root" do
visit root_url
click_on 'New Project'
expect(current_path).to eq(new_project_path)
fill_in "Name", with: "My New Cool Project"
fill_in "Details", with: "Praise the sun!"
select "urgency_fire", :from => "Urgency"
select "status_in_progress", :from => "Status"
select "ABC - abc", :from => "Client"
fill_in "Due date", with: Time.now.to_s
find('input[name="commit"]').click
expect(current_path).to eq(root_path)
expect(page).to have_text("My New Cool Project")
end
end
ここでは、テスト出力である:ここで
1) Creating a new project saves new project and returns to list of projects on root
Failure/Error: %td= project.client.code
ActionView::Template::Error:
undefined method `code' for nil:NilClass
# ./app/views/dashboard/index.html.haml:17:in `block in _app_views_dashboard_index_html_haml__3165971454273894605_70112485977840'
# ./app/views/dashboard/index.html.haml:15:in `_app_views_dashboard_index_html_haml__3165971454273894605_70112485977840'
# ./spec/features/new_project_form_spec.rb:14:in `block (2 levels) in <top (required)>'
# ------------------
# --- Caused by: ---
# NoMethodError:
# undefined method `code' for nil:NilClass
# ./app/views/dashboard/index.html.haml:17:in `block in _app_views_dashboard_index_html_haml__3165971454273894605_70112485977840'
は、ダッシュボードコントローラです:
class DashboardController < ApplicationController
before_action :authenticate_user!
def index
@projects = Project.includes(:client).all
end
end
そしてここでは、ダッシュボード、HTML(HAMLです):
.container
.row
.col-sm-12
%p= link_to 'New Project', new_project_path, title: 'New Project'
%table
%thead
%tr
%th Client
%th Project
%th Urgency
%th Status
%th Due Date
%th Assigned To
%tbody
- @projects.each do |project|
%tr
%td= project.client.code
%td= link_to project.name, project
%td= project.urgency
%td= project.status
%td= project.due_date
%td= project.user.count
そして、あなたはあなたにログインしていない場合、私は、ページ内のログを参照してくださいので、設定した経路を持っている、とあなたはあなたの中にログインしている場合は、ダッシュボードを参照してください。
Rails.application.routes.draw do
# devise gem routes
devise_for :users
# Dashboard
authenticated :user do
root :to => 'dashboard#index'
end
# Login
devise_scope :user do
root to: "devise/sessions#new"
end
# Projects
resources :projects
# Send all unknown pages to root
if ENV["RAILS_ENV"] == "production"
get '*path' => redirect('/')
end
end
おかげでグレッグを行う必要があります! –