2017-11-06 3 views
0

저는 rspec 및 capybara를 처음 사용하고 테스트 시나리오를 천천히 확장하여 학습합니다.Rspec : 로그인 한 사용자와 다른 사용자가 작성한 설명

내가 로그인 한 후 작성한 주석 (예 : 친구)이 다른 테스트 사용자 (예 : Test1)가 작성한 것처럼 인쇄되는 이유를 이해할 수 없습니까?

(I 브라우저에서 확인하고 현재 사용자가 만든 같은 댓글이 표시됩니다)

require 'rails_helper' 

describe 'navigate' do 

    let!(:user) { FactoryGirl.create(:user) } 
    let!(:friend) { FactoryGirl.create(:friend) } 
    let!(:article) { 
    Article.create(title:"To test notifications", summary: "Summary of article", description: "Description of article.", status: "published", user_id: user.id) 
    } 

    describe 'notification created if other use comments on article' , js: true do 

    before do 
     login_as(friend, :scope => :user) 
     visit article_path(article) 
     fill_in 'comment[content]', with: "Some comments" 
     click_on "Add Comment" 
     logout(:friend) 
     login_as(user, :scope => :user) 
    end 

    it "renders notification on dropdown list" do 
     expect(page).to have_css("#notificationList", text: friend.name + " posted a Comment on " + article.title) 
     expect(page).to have_css("#notification-counter", text: "1") 
    end 

    it "render notification on notifiction index page" do 
     visit notifications_path 
     expect(page).to have_css("#notifications-container", text: friend.name + " posted a Comment on " + article.title) 
    end 

    end 

end 

나도 방법을 아래에 시도했지만 내가 응용 프로그램에 로그인 할 수없는이기 때문에 테스트가 실패합니다.

2) login_as(friend, :scope => :friend)

사용자 공장 login_as(friend, :scope => :user) 교체 :

FactoryGirl.define do 
    sequence :email do |n| 
    "test#{n}@example.com" 
    end 

    factory :user do 
    sequence(:name) { |n| "Test#{n}" } 
    email { generate :email } 
    password "asdfasdf" 
    password_confirmation "asdfasdf" 
    end 

    factory :friend, class: "User" do 
    name 'Friend' 
    email { generate :email } 
    password "asdfasdf" 
    password_confirmation "asdfasdf" 
    end 

end 

답변

0

이다 일으키는 컨트롤러/뷰에서 논리 오류가없는 당신을 가정하면 (그것은 dev에 모드에서 작동) 이는 테스트에서 작업이 완료되기를 기다리지 않고 발생했을 가능성이 높습니다. 가장 쉬운 방법은 sleep 2click_onlogout 뒤에 넣는 것입니다. 이 문제를 해결한다면 적절한 솔루션은 작업이 완료 될 때 나타나는 것들에 대한 기대를 추가하는 것입니다

before do 
    login_as(friend, :scope => :user) 
    visit article_path(article) 
    fill_in 'comment[content]', with: "Some comments" 
    click_on "Add Comment" 
    expect(page).to have_text "Comment Added" # whatever is shown once "Add Comment" succeeds 
    logout(:friend) 
    expect(page).to have_text "You have logged out" # whatever change indicates logout has completed 
    login_as(user, :scope => :user) 
end 

click_on 어떤 행동 (무엇 사람들을 알 수있는 방법이 없습니다 기다릴 보장되지 않기 때문입니다 동작은 클릭에 의해 트리거 될 수 있음). 귀하의 경우 즉, "Add Comment (추가 설명)"를 클릭하면 즉시 logout이 발생하고 그 다음에 login_as으로 전화합니다. login_as은 지정된 사용자가 다음 요청에 대해 로그인 한 것처럼 행동하는 워너/devise 미들웨어에게 알려줌으로써 작동합니다. 따라서 "Add Comment"클릭으로부터 요청이 처리되기 전에 발생하면 "Add Comment" friend이 아닌 user처럼 처리 요청. 당신 사이에 잠이나 기대를 더함으로써 올바른 사용자로서 행동을 취할 수있는 시간을주고 시험을 계속하기 전에 완료되었는지 확인하십시오.

는 또한 유증/소장은 실제로 페이지를 방문하지 않습니다 login_as을 제공, 그래서 당신이 사용하고있는 login_as의 버전 인 경우 당신은 아마 그래서 페이지가 user으로 방문 도착 첫 번째 테스트의 시작 부분에 visit <somewhere> 필요

마지막으로 참고, 당신이 당신의 공장에서 중복을 많이 가지고 - 당신이

FactoryGirl.define do 
    sequence :email do |n| 
    "test#{n}@example.com" 
    end 

    factory :user do 
    sequence(:name) { |n| "Test#{n}" } 
    email { generate :email } 
    password "asdfasdf" 
    password_confirmation "asdfasdf" 

    factory :friend do 
     name 'Friend' 
    end 
    end 
end 

https://github.com/thoughtbot/factory_bot/blob/master/GETTING_STARTED.md#inheritance

+0

안녕하세요로 쓰기 더 나을 것, 그쪽으로 조언을 주셔서 감사합니다! 나는 로그 아웃하기 전에 Friend의 의견이 성공적으로 제출되었는지 확인하기 위해 click_on "comments"뒤에 expect (page) .to have_content "Some comments"를 배치했습니다. 그러나, 나는 지금 logout (: friend)을 expect 문 다음에 배치 했음에도 불구하고 Friend를 로그 아웃 할 수 없다. 로그 아웃하고 수동으로 로그인하면 테스트가 통과됩니다. (예 : 로그 아웃 버튼을 클릭하고 로그인 양식을 입력하십시오) – doyz

+0

문서 작성자에게 알림을 실시간으로 업데이트 할 수 있도록 조치 할 수있는 설정이 있지만이 문제와 관련이 있는지 의심 스럽습니까? – doyz

+0

@diana'logout'이'devise' /'warden'에서 나온 것이면 매개 변수 (사용자가 아닌)로 범위를 가져옵니다. 그러므로 여러분의 경우'logout (: user)'여야합니다 - 아니라 : 친구 : 당신은 : 친구 범위가 없기 때문에 단지 '친구'사용자 인스턴스 –

관련 문제