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
。請幫幫我。
請顯示您的'routes.rb'文件。 –
這裏是routes.rb。請。 –
資源:請求,但:[:編輯,:更新]做 資源:承諾,只:[:創建,:顯示,:破壞],淺:真做 資源:評論,只:[:創建] 結束 結束 –