使用rspec / devise进行登录集成测试 [英] Login integration test with rspec/devise

查看:132
本文介绍了使用rspec / devise进行登录集成测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在使用Rspec和capybara,以及设计用户认证



这是我的代码:



requests / authentications_spec.rb

 描述'登录'do 
之前{访问root_path}
主题{page}

描述'应该能够登录'do
before $
user = FactoryGirl.create(:user)
fill_in:user_email,with:user.email
fill_in:user_password,with :user.password
click_on'登录'
end
it {should have_link'注销'}
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}
密码'foobar'
password_confirmation'foobar'
created_at Time.now
updated_at Time.now
end
end

_login_form.html.erb,
此表单在应用程序中呈现布局标题。

 <%= form_for(资源,as:resource_name,url:session_path(resource_name),html:{id :'login-mini-form'})do | f | %GT; 
<%= f.email_field:电子邮件,占位符:'您的电子邮件'%>
<%= f.password_field:密码,占位符:'******'%>
<%= f.submit登录,id:'login-button'%>

<% - 如果devise_mapping.rememberable? - %GT;
<%= f.check_box:remember_me%> <%= f.label:remember_me%>
<% - end - %>

<%end%>

在布局中我有这样的东西:



如果user_signed_in是

  
render'devise / menu / logout_button'
else
render'devise / menu / login_form'
end

测试正在给我

  1)用户应该能够记录在有效的凭据
失败/错误:它{应该有_link'注销'}
期望链接注销返回一些
#./spec/requests/authentications_spec.rb:117 :在$($)

$ block $($ 6)

完成0.71406秒
8个例子,2个失败,2个待处理

我不知道为什么我的测试不通过。任何想法?



非常感谢!



编辑:我得到相同的行为测试注册: code> expect {click_button register_button}。to change(User,:count).by 1 ,测试返回:

 失败/错误:期望{click_button register_button}。更改(用户,计数)。1 
计数应该已更改1,但更改为0


解决方案

所以,我发现没有工作:

 描述'登录'do 
之前{visit root_path}
subject {page}

描述'应该能够登录'do
before
user = FactoryGirl.create(:user)
fill_in:user_email,with:user.email
fill_in:user_password, with:user.password
click_on'登录'
end
it {should have_link'注销'}
end

正确代码实际上是:

 描述'登录'do 
before {visit root_path}
subject {

描述'应该能够登录'do
之前
user = FactoryGirl.create(:user)
fill_in'user_email',其中: user.email
fill_in'user_password',其中:user.password
click_on'登录'
end
it {should have_link'注销'}
end
end
end

似乎Capybara fill_in方法不采取符号ids只是字符串的参数。
傻我。


I'm having troubles making an integration test, for the user login in my rails application, pass.

I'm using Rspec and capybara, as well as devise for user authentication

Here is my code :

requests/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

factories/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 form is rendered in the application layout header.

<%= 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 %>

In the layout I have something like that :

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

The test is giving me

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

I don't get why my test is not passing. Any ideas ?

Thanks a lot !

Edit : I get the same behavior testing the registration with : expect { click_button register_button }.to change(User, :count).by 1, the test returns :

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

解决方案

So, I have found what was not working :

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

The correct code is in fact :

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

It seems Capybara fill_in method doesn't take a symbol as an argument for ids but only strings. Silly me.

这篇关于使用rspec / devise进行登录集成测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆