2016-12-13 206 views
0
feature "comment" do 
given(:user) do 
     build(:user) 
end 
background do 
    user1=create(:user) 
    user1.id=1 
    login_as(user1)  
end 
scenario "can create comment" do 
    @undertake=create(:undertake) 
    visit undertake_path(@undertake) 
    within("form#undertake-form-test") do 
     fill_in "content" , with: "heyheyhey" 
    end 
    click_button 'send-btn' 
    expect(page).to have_content 'heyheyhey' 
end 
end 

這是spec/features/comment_spec.rb。這下面是controllers/undertakes_controller.rb。rspec,未定義方法`id'爲零:NilClass

class UndertakesController < ApplicationController 
    def show 
    @undertake=Undertake.find_by(id: params[:id]) 
    @comment=current_user.comments.new 
end 

以下是views/undertaking/show.html.erb。

<p><%= @undertake.id %></p> 

and spec/factories/undertakes.rb。

FactoryGirl.define do 
    factory :undertake do 
    association :ask 
    association :user 
    id 1 
    user_id 2 
    ask_id 1 
    title "MyString" 
    content "MyText" 
    result false  
    end 
end 

的routes.rb

resources :asks , except:[:edit, :update] do 
    resources :undertakes , only:[:create , :show , :destroy] , shallow: true do 
    resources :comments , only:[:create] 
    end 
end 

現在,爲什麼我有錯誤ActionView::Template::Error:undefined method id for nil:NilClass。請幫幫我。

+0

請顯示您的'routes.rb'文件。 –

+0

這裏是routes.rb。請。 –

+0

資源:請求,但:[:編輯,:更新]做 資源:承諾,只:[:創建,:顯示,:破壞],淺:真做 資源:評論,只:[:創建] 結束 結束 –

回答

2

這裏有很多事情可能是潛在的原因,如果不是直接錯誤的,那麼它們是非常單一的。

首先關掉名稱undertake是錯的。請使用名詞形式Undertaking代替模型名稱。

千萬不要使用find_by(id: params[:id])。相反,使用find(params[:id]),因爲它將引發ActiveRecord::RecordNotFoundError並呈現404頁面(如果未找到該記錄而不是用零錯誤炸燬)。

class UndertakingsController < ApplicationController 
    def show 
    @undertaking = Undertaking.find(params[:id]) 
    @comment = @undertaking.comments.new 
    end 
end 

你也應該建立從@undertaking評論 - 因爲它使孩子的玩了的惡意用戶欺騙不通過表單傳遞用戶ID。

創造記錄時而是從分配會話的用戶:

class CommentsController 
    # this assumes you are using Devise 
    before_action :authenticate_user! 
    def create 
    @comment = Comment.new(comment_params) do |c| 
     c.user = current_user 
    end 
    # ... 
    end 
end 

這是你會怎麼寫地道的規範。請注意使用備忘錄let助手和事實上,您從從不分配ID記錄。這由數據庫完成。試圖手動完成它只會搞砸了。

require 'rails_helper' 
RSpec.describe 'Comments' do 
    let(:current_user) { create(:user) } 
    let(:undertaking) { create(:undertaking) } 

    background do 
    login_as(current_user) 
    end 

    scenario "can create a comment" do 
    visit undertaking_path(undertaking) 
    # Avoid using CSS selectors and instead write specs 
    # based on what the user sees as it makes specs brittle 
    within("form#undertake-form-test") do 
     fill_in "content" , with: "heyheyhey" 
     click_button 'Create comment' 
    end 
    expect(page).to have_content 'heyheyhey' 
    end 
end 

使用let而不是@instance變量。在編寫功能,請求和控制器規格時,您需要使用FactoryGirl.create而不是build,因爲後者不會插入到數據庫中,並且該記錄實際上不會存在於您的Rails應用程序中。

此外,您的工廠定義嚴重損壞。工廠的整個想法是它應該創建獨特的,有效的記錄。切勿在工廠中設置ID。

FactoryGirl.define do 
    factory :undertaking do 
    title "MyString" 
    content "MyText" 
    user # just reference the factory if you REALLY need the association to exist 
    ask # just reference the factory if you REALLY need the association to exist 
    result false # this should probally be set through a DB default instead! 
    end 
end 
+0

偉大的建議!:) –

+0

謝謝你的建議!我會這樣做! –

相關問題