简体   繁体   English

使用rspec / devise进行登录集成测试

[英]Login integration test with rspec/devise

I'm having troubles making an integration test, for the user login in my rails application, pass. 我在进行集成测试时遇到了麻烦,因为我的Rails应用程序中的用户登录通过了。

I'm using Rspec and capybara, as well as devise for user authentication 我正在使用Rspec和capybara,以及用于用户身份验证的设备

Here is my code : 这是我的代码:

requests/authentications_spec.rb 请求/身份验证_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 factory / 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. _login_form.html.erb,此表单在应用程序布局标头中呈现。

<%= 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 : 编辑:我得到的行为与测试注册的行为相同: 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

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. 似乎Capybara fill_in方法不会将符号作为id的参数,而只是将字符串作为参数。 Silly me. 傻我

You didn't provide a subject for your expectation, so it looks like RSpec is using the result of the last expression. 您没有提供期望的主题,因此看起来RSpec正在使用最后一个表达式的结果。 That last expression is your click_button , which doesn't return anything. 最后一个表达式是您的click_button ,它不返回任何内容。 You can't call should on nothing. 你不能调用should就没事了。

To fix this, you just need to specify that the page should have said link. 要解决此问题,您只需要指定页面应具有所述链接即可。 I use Capybara and Selenium-Webdriver, so for me it'd look like page.should have_link... . 我使用Capybara和Selenium-Webdriver,因此对我来说就像page.should have_link... My RSpec-fu is not strong enough to know if you should use page too, or substitute response , session , or something else. 我的RSpec-fu不够强大,无法知道您是否也应该使用page ,还是用responsesession或其他方式代替。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM