2017-04-07 10 views
0

私はユニットテストには本当に新しいので、私はそれの周りに私の頭を包んでしようとしています。したがって、ユーザーがtitledescriptionと入力してCreate Articleをクリックすると記事が作成されるはずですが、ログインしたユーザーだけがこの操作を実行できるので、これをテストする必要があります。Ruby on Rails - テストケースにユーザを追加する

私はこれは私が考えているものをこれに新しいですしておりますので、

  1. ユーザーは(ログインしている場合はどのようにシステムチェックthatsのように私はセッションに保存
  2. 最初のユーザーを作成します。しかし、私はまた、ブラウザではないので、このロジックが動作しないかもしれないと思っています)、どのように私はログインしたユーザーとしてフォームを送信するのですか?ここ

私は、コマンドにrspec spec/features/add_article_spec.rb

を実行すると、私の試みは

require 'rails_helper' 

    RSpec.feature 'adding article' do 
     scenario 'allow user to add an article' do 
     @user = User.create(:email => "[email protected]", :password => 'password', :username => 'saadia1') 
     session[:user_id] = @user.id 

     visit new_article_path 

     # @article = Article.user.to eql(@user = User.find_by(id: 6)) 
     fill_in "Title", with: "My Title" 
     fill_in "Description", with: "My description" 


     click_on("Create Article") 

     expect(page).to have_content("My Title") 
     expect(page).to have_content("My description") 

     end 
    end 

である私は

Failures:

1) adding article allow user to add an article Failure/Error: session[:user_id] = @user.id

NameError: 
    undefined local variable or method `session' for #<RSpec::ExampleGroups::AddingArticle:0x007f89285e32e8> 
# ./spec/features/add_article_spec.rb:6:in `block (2 levels) in <top (required)>' 

Finished in 0.0197 seconds (files took 1.35 seconds to load) 1 example, 1 failure

Failed examples:

rspec ./spec/features/add_article_spec.rb:4 # adding article allow user to add an article

を参照してくださいだから私の質問は、私がログインして記事を追加するにはどうすればよいですユーザー?私は本当にこれに関する助けを感謝します。

答えて

0

はい工夫がテストのためのいくつかのヘルパーを提供している場合は、認証のために工夫を使用しているにも、これはあなたが工夫のsign_inヘルパーメソッドを使用するのに役立ちます、そしてあなたが必要としませウィル

config.include Devise::Test::ControllerHelpers, :type => :controller あなたrail_helper.rbにこの行を含めます現在セッションを使用しています。詳細については、linkを参照してください。

+0

ありがとうございます、はい私はdeviseを使用しています。私は試して戻ってくるだろう – Saadia

0

これが最初のテストケースの1つであるため、テストケースを作成しました改善されましたので、お気軽にご確認ください

require 'rails_helper' 

RSpec.feature 'adding article' do  
    scenario 'allow user to add an article' do 
    user = FactoryGirl.create(:user) 
    visit login_path 
    fill_in 'Email', with: user.email 
    fill_in 'Password', with: user.password 
    click_button 'Log in' 
    expect(page).to have_content('You have successfully logged in') 


    visit new_article_path 

    fill_in "Title", with: "My Title" 
    fill_in "Description", with: "My description" 


    click_on("Create Article") 

    expect(page).to have_content("My Title") 
    expect(page).to have_content("My description") 

    end 
end 

これは私の工場

# spec/factories/users 
FactoryGirl.define do 
    factory :user do 
    sequence(:username) { |n| "user#{n}" } 
    password 'password' 
    sequence :email do |n|n 
    "user_#{n}@example.com" 
    end 
    end 
end 

Finished in 0.4176 seconds (files took 2.54 seconds to load) 1 example, 0 failures

である私はまだそれが別のシナリオでは、ユーザーがログインするために、すべての可能な場合、またはこのすべてが、1つのシナリオの一部である必要がありますかどうかを知りたいです。

関連する問題