2016-12-13 22 views
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です。rspec定義されていないメソッド "id" for nil:NilClass

   #<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: "[email protected]", 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: "[email protected]", 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にnilではありませんが、なぜ@undertakingは表示されないのですか?

+0

申し訳ありませんが、私はそれを覚えています。 –

+0

ルートファイルを共有してください。そして確かに、あなたはそのコントローラーのインデックスアクションも持っていますか? – Kkulikovskis

答えて

1

あなたのコントローラに使用されている名前と関係があると思います。大会は/ show.html.erbを引き受けるのではなく、その見解のために/ show.html.erbです。私はまた最後に、私はすべての私のルートも正しいネーミングを持っていることを確認します

class UndertakingsController < ApplicationController 

代わり

class UndertakingController < ApplicationController 

を使用します。希望が役立ちます。幸運

関連する問題