2013-12-10 67 views
1

在我的認證規範,我有與軌道4

require 'spec_helper' 

describe "Authentication" do 

    subject { page } 

    describe "Signup page" do 
     before { visit new_user_registration_path } 

     let(:submit) { "Create my account" } 

     describe "with invalid information" do 
      it "should not create a user" do 
      expect { click_button submit }.not_to change(User, :count) 
      end 

      describe "after submission" do 
      before { click_button submit } 

      it { should have_content('Sign up') } 
      it { should have_content('error') } 
      end 
     end 

     describe "with valid information" do 
      before do 
      fill_in "First name", with: "Example" 
      fill_in "Last name", with: "Lastname" 
      fill_in "Username",  with: "exampleguy" 
      fill_in "Email",  with: "[email protected]" 
      fill_in "Password",  with: "foobar1234" 
      end 

      it "should create a user" do 
      expect { click_button submit }.to change(User, :count).by(1) 
      end 

      #describe "after saving the user" do 
      #before { click_button submit } 
      #let(:user) { User.find_by(email: '[email protected]') } 

      #it { should have_title(user.first_name) } 
      #it { should have_selector('div.flash_success', text: 'Welcome') } 
     # end 
     end 

     it { should have_content('Sign up') } 
    end 


    describe "Signin page" do 

     before { visit new_user_session_path } 

     describe "with invalid information" do 
      before { click_button "Sign in" } 

      it { should have_content('Sign in') } 
      it { should have_selector('div.flash_alert', text: "Invalid") } 

     end 

     describe "with valid information" do 
      let(:user) { FactoryGirl.create(:user) } 
      before do 
       fill_in "Email", with: user.email 
       fill_in "Password", with: user.password 
       click_button "Sign in" 
      end 

      it { should_not have_selector('a', text: 'Sign up')} 
      it { should_not have_selector('a', text: 'Sign in')} 
      it { should have_selector('a', text: 'Profile') } 
      it { should have_selector('a', text: 'Sign Out') } 
      it { should have_selector('div.flash_notice', text: "Signed in successfully.") } 

     end 

     it { should have_content('Sign in') } 
    end 
end 

而且在factories.rb試驗設計Rspec的跡象

FactoryGirl.define do 
    factory :user do 
    first_name "Example" 
    last_name "User" 
    username "exampleuser" 
    email  "[email protected]" 
    password "foobar1234" 
    end 
end 

每個測試從登錄-with有效信息組工作分開的測試。我不知道爲什麼,它在瀏覽器中正常工作。有沒有人遇到過這樣的問題?

+0

你可以添加失敗的行嗎? – tyler

+0

是那些在登錄頁面 - >有效的信息,例如它{should_not have_selector('a',text:'註冊')}以及該測試組中的其餘部分。 – Skyalchemist

回答

8

如果您使用的是設計確認模塊,您必須添加user.confirm!到您的登錄方法。
將這個支持/ utilities.rb

def sign_in(user) 
    user.confirm! 
    visit new_user_session_path 
    fill_in "Email", with: user.email 
    fill_in "Password", with: user.password 
    click_button "Sign in" 
end 

在你的描述塊 「用有效的信息」 改變這種

before do 
    fill_in "Email", with: user.email 
    fill_in "Password", with: user.password 
    click_button "Sign in" 
end 

了這一點。

before { sign_in(user) } 
+0

就這一行!我已經放棄了。謝謝:) – Skyalchemist

+1

而不是'support/utilities.rb',它應該不是'spec/support/utilities.rb'嗎? –

+0

嗯,你明白我想說的是什麼。 –