首页 文章

用于nil的rspec undefined方法`id':NilClass

提问于
浏览
0
require 'rails_helper'

      feature "comment" do
        given(:current_user) do
          create(:user)
        end
        given(:undertaking) do
           create(:undertaking)
        end
        background do
         login_as(current_user)
        end
        scenario "can create comment" do
          #below two because undertaking = user_id:2 & asking_id:1
          create(:user)
          create(:asking)
          p undertaking
          p Asking.find(1)
          p User.find(2)
          p User.find(1)
          p Undertaking.all
          visit undertaking_path(undertaking)
          expect(current_path).to eq undertaking_path(1)
          within("form#undertake-form-test") do
           fill_in "content" , with: "heyheyhey"
          end
          click_button 'Send'
          expect(page).to have_content 'heyheyhey'
        end
       end

这是spec / features / comment_spec.rb . 以下是结果命令rspec .

#<Undertaking id: 1, title: "MyString", content: "MyText", result: false, user_id: 2, asking_id: 1, created_at: "2016-12-13 15:07:08", updated_at: "2016-12-13 15:07:08">
              #<Asking id: 1, content: "MyText", fromlang: "MyString", tolang: "MyString", usepoint: 1, finished: false, title: "MyString", deadline: nil, user_id: 1, created_at: "2016-12-13 15:07:08", updated_at: "2016-12-13 15:07:08">
              #<User id: 2, email: "shiba.hayato2@docomo.ne.jp", created_at: "2016-12-13 15:07:08", updated_at: "2016-12-13 15:07:08", provider: nil, uid: nil, name: "Shiruba", occupation: "大学生", age: 10, sex: "男性", content: "heyheyheyeheyeheye", skill: "日本語検定3級", picture: "/assets/default_user.jpg", point: 500, country: "Japan", language1: "Japanese", language2: "Korea", language3: "English">
             #<User id: 1, email: "shiba.hayato1@docomo.ne.jp", created_at: "2016-12-13 15:07:08", updated_at: "2016-12-13 15:07:08", provider: nil, uid: nil, name: "Shiruba", occupation: "大学生", age: 10, sex: "男性", content: "heyheyheyeheyeheye", skill: "日本語検定3級", picture: "/assets/default_user.jpg", point: 500, country: "Japan", language1: "Japanese", language2: "Korea", language3: "English">
            #<ActiveRecord::Relation [#<Undertaking id: 1, title: "MyString", content: "MyText", result: false, user_id: 2, asking_id: 1, created_at: "2016-12-13 15:07:08", updated_at: "2016-12-13 15:07:08">]>
            F

            Failures:

            1) comment can create comment
                Failure/Error: <%= @undertaking.id %>

                ActionView::Template::Error:
                      undefined method `id' for nil:NilClass

以下是undertaking_controller.rb .

class UndertakingController < ApplicationController
              def show
               @undertaking=Undertaking.find(params[:id])
               @comment=Comment.new do |c|
                c.user=current_user
               end
              end
       end

以下是承诺/ show.html.erb .

<%= @undertaking.id %>

为什么我有错误?虽然Undertaking.first在spec / features / comment_spec.rb中不是零,但为什么@undertaking只是nil?请帮助我 .

1 回答

  • 1

    我认为它与用于控制器的命名有关 . 该约定是视图的承诺/ show.html.erb而不是承诺/ show.html.erb . 我也会用

    class UndertakingsController < ApplicationController
    

    代替

    class UndertakingController < ApplicationController
    

    最后,我会检查我的所有路线是否也有正确的命名 . 希望有所帮助 . 祝好运

相关问题