2017-04-07 3 views
0

나는 정말로 유닛 테스트를 처음 접했고 그 주위를 감싸하려고 노력 중이다. 따라서 사용자가 titledescription을 입력하고 Create Article을 클릭하면 기사 양식을 작성해야하지만 상황은 로그인 한 사용자 만이 작업을 수행 할 수 있으므로 테스트해야합니다. Ruby on Rails - 테스트 케이스에 사용자 추가하기

나는이 새로운 오전부터

은 그래서 이것은 내가 생각하고 무엇

방법 시스템 검사를 먹으 렴으로 사용자가 로그인하면 내가 세션에 저장을 먼저 사용자
  • 을 생성합니다
    1. (하지만 그런 다음이 논리가 작동하지 않을 수도있는 브라우저가 아니라고 생각합니다.) 그러면 로그인 한 사용자로 양식을 제출하는 방법은 무엇입니까? 내가

      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

      그래서 내 질문에 내가이 로그인으로 기사를 어떻게 추가합니까 것을 볼 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 
    

    입니다 사용자? 나는 이것에 대한 도움을 정말로받을 것이다.

    답변

    0

    예 유증 테스트에 대한 몇 가지 헬퍼를 제공하는 경우 인증을 위해 고안 사용하고 또한 이것이 당신이 유증의 sign_in 도우미 메서드를 사용하는 데 도움이 될 것입니다, 당신은 필요가 없습니다 줘야

    config.include Devise::Test::ControllerHelpers, :type => :controller 당신의 rail_helper.rb이 줄을 포함 현재하고있는대로 세션을 사용하십시오. 자세한 내용은 link을 참조하십시오.

    +0

    감사합니다. 예, 사용하고 있습니다. 나는 다시하려고 노력할 것이다. – Saadia

    0

    이것이 첫 번째 테스트 케이스 중 하나이기 때문에 테스트 케이스를 작성하는 방법입니다. 어떻게 이것이 될 수 있는지 잘 모르겠습니다. 개선되었으므로 언제든지 검토 해주십시오.

    이는 다른 시나리오에서 사용자를 로그인하는 모든 수 또는 모든이 하나 개의 시나리오의 일부가 될 경우 나는 아직도

    # 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

    알고 싶은 내 공장입니다.