2012-07-03 115 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