2016-10-26 2 views
0

저는 레일에서 새로 왔으며 capybara rspec 및 공장 아가씨와 함께 내 사기 도구 사용자를 테스트하고 있습니다.테스트 데비안 capybara rspec 및 공장 걸 사용하기

여기 사양 코드입니다 :

require 'rails_helper' 
RSpec.describe User, type: :request do 
    it "displays the user's email after successful login" do 
    user = FactoryGirl.create(:user, email: '[email protected]', password: 'password') 
    visit root_url 
    fill_in 'Email', with: '[email protected]' 
    fill_in 'Password', with: 'password' 
    click_button 'Log in' 
    expect page.has_content?('jdoe') 
    end 
end 

문제는 여기에

expect page.has_content?('jdoe') No matter what i put instead 'jdoe' test works perfectly without any errors.

에있는 공장이다 :
FactoryGirl.define do 
    factory :user do 
    email '[email protected]' 
    password 'password' 
    password_confirmation 'password' 
    end 
end 

어쩌면 내가 뭔가 또는 잘못된 방향으로 가고를 놓쳤다. 여기서 어떻게 테스트해야합니까? 여기

+1

당신은 jdoe ' –

+0

1 예를 테스트 @의 test.com'' 0 실패 'have_content .TO (페이지) 기대'시도 할 수 jdoe '도 녹색이다, 나는 정확하게 이해했다? –

+0

그래서 have_content .TO (페이지) 기대'테스트 ''는 것과 같이, –

답변

1
# spec/features/sessions_spec.rb 
require 'rails_helper' 
RSpec.feature "Sessions" do 
    scenario "displays the user's email after successful login" do 
    user = FactoryGirl.create(:user) 
    visit root_url 
    fill_in 'Email', with: user.email 
    fill_in 'Password', with: user.password 
    click_button 'Log in' 
    expect(page).to have_content("Signed in successfully") 
    # will give a false postitive if form is rerended? 
    expect(page).to have_content(user.email) 
    end 
end 

몇 가지 :

  1. 테스트를 끝과 끝의 feature 아닌 요구 사양을 사용합니다.
  2. 오브젝트 속성을 하드 코딩하지 마십시오. 공장의 전체적인 관점은 테스트가 잔여 상태로 인해 오 탐지를하지 않도록 고유 한 데이터를 생성하는 것을 공장에서 처리한다는 것입니다.
  3. expect page.should 대신 expect(page).to을 사용하십시오. expect(page).to은 제목을 설정하고 일치하지 않을 경우 오류 메시지에 페이지의 텍스트를 표시하는 RSpec 일치 프로그램을 사용합니다.
+2

오타가 있습니다. 'expect (page) .to have_content (user.email)'(물음표없이) –

+0

1) 세션 로그인 성공 후 사용자의 이메일 표시 오류/오류 : expect (page) .to have_content? (user.email) 테스트 실행 후'has_content ?? '에러에 응답하는 # . –

+0

@OleksandrAvoyants –

관련 문제