2012-07-03 144 views
0

我有麻煩進行集成測試,用戶登錄在我的rails應用程序中傳遞。使用rspec/devise登錄集成測試

我使用Rspec的水豚和,以及制定用戶認證

這裏是我的代碼:

請求/ authentications_spec.rb

describe 'log in' do 
    before { visit root_path } 
    subject { page } 

    describe 'should be able to log in' do 
    before do 
     user = FactoryGirl.create(:user) 
     fill_in :user_email, with: user.email 
     fill_in :user_password, with: user.password 
     click_on 'Log in' 
    end 
    it { should have_link 'Log out' } 
    end 
end 

工廠/ user_factory.rb

FactoryGirl.define do 
    factory :user do 
    sequence(:first_name) { |n| "FirstName#{n}" } 
    sequence(:last_name) { |n| "LastName#{n}" } 
    sequence(:email) { |n| "foo#{n}@example.com" } 
    password    'foobar' 
    password_confirmation 'foobar' 
    created_at   Time.now 
    updated_at   Time.now 
    end 
end 

_login_form.html.erb, This fo rm在應用程序佈局標題中呈現。

<%= form_for(resource, as: resource_name, url: session_path(resource_name), html: { id: 'login-mini-form' }) do |f| %> 
    <%= f.email_field :email,  placeholder: 'Your email' %> 
    <%= f.password_field :password, placeholder: '******' %> 
    <%= f.submit "Log in", id: 'login-button' %> 

    <%- if devise_mapping.rememberable? -%> 
    <%= f.check_box :remember_me %> <%= f.label :remember_me %> 
    <%- end -%> 

<% end %> 

在佈局我有類似的東西:

if user_signed_in? 
    render 'devise/menu/logout_button' 
else 
    render 'devise/menu/login_form' 
end 

測試是給我

1) User should be able to log in with valid credentials 
    Failure/Error: it { should have_link 'Log out' } 
     expected link "Log out" to return something 
    # ./spec/requests/authentications_spec.rb:117:in `block (6 levels) in <top (required)>' 

Finished in 0.71406 seconds 
8 examples, 2 failures, 2 pending 

我不知道爲什麼我的測試不及格。有任何想法嗎 ?

非常感謝!

編輯:我獲得相同的行爲與測試報名:expect { click_button register_button }.to change(User, :count).by 1,測試的回報:

Failure/Error: expect { click_button register_button }.to change(User, :count).by 1 
     count should have been changed by 1, but was changed by 0 
+0

請問我們有完整的RSpec輸出嗎? – tsm

+0

我編輯了我的開場白:) –

+0

你可以檢查日誌記錄嗎?檢查'log/test.log'是否有任何明顯的錯誤。 – nathanvda

回答

1

所以,我發現了什麼是不工作:

describe 'log in' do 
    before { visit root_path } 
    subject { page } 

    describe 'should be able to log in' do 
    before do 
     user = FactoryGirl.create(:user) 
     fill_in :user_email, with: user.email 
     fill_in :user_password, with: user.password 
     click_on 'Log in' 
    end 
    it { should have_link 'Log out' } 
    end 

正確的代碼就是:

describe 'log in' do 
    before { visit root_path } 
    subject { page } 

    describe 'should be able to log in' do 
     before do 
     user = FactoryGirl.create(:user) 
     fill_in 'user_email', with: user.email 
     fill_in 'user_password', with: user.password 
     click_on 'Log in' 
     end 
     it { should have_link 'Log out' } 
    end 
    end 
end 

看來水豚FILL_IN方法並不需要一個符號作爲一個參數ID,但只有字符串。 傻我。

0

您沒有提供您的期望的對象,所以它看起來像RSpec的是使用的結果最後一個表達。最後一個表達式是click_button,它不返回任何內容。什麼也不能撥打should

要解決這個問題,你只需要指定頁面應該有說的鏈接。我使用水豚和Selenium-Webdriver,所以對我來說它看起來像page.should have_link...。我的RSpec-fu不夠強,無法知道您是否也應該使用page,或者用response,session或其他。

+0

不能,因爲@paperwork已經聲明瞭'subject',rspec將使用它。 – nathanvda