0

将 rails 升级到 v4、rspec 和其他 gem 后,rspec 无法登录。

常规登录工作正常,只是 rspec 失败。我怀疑它可能与 cookie 有关,由于config.secret_tokenand config.secret_key_base,目前只设置了第一个,但我也尝试设置另一个。

在我输入之后的规范中sign_insave_and_open_page我没有在打开的页面上登录。但是它确实达到redirect_back_or user了,所以我知道这个过程可以正常工作,问题是实际的登录/cookies。所以我想它必须与cookie/remember_token有关,对吧?


规范的结果错误之一:

  2) signin with valid information 
     Failure/Error: scenario{ should have_link('Sign out', href:signout_path)}
     Capybara::ExpectationNotMet:
       expected to find link "Sign out" but there were no matches
     # ./spec/features/authentication_pages_spec.rb:46:in `block (3 levels) in <top (required)>'

认证规范:

feature  "signin" do
  subject {page}

  before{ visit signin_path }

  describe "with valid information" do
    let(:user) {FactoryGirl.create(:user)}

    before {sign_in user}

    scenario{ should have_link(user.name,  href:user_path(user))}
    scenario{ should have_link('Sign out', href:signout_path)}
  end
end

直到.rb:

def sign_in(user)
  visit signin_path
  fill_in "Email-signin",     with: user.email
  fill_in "Password-signin",  with: user.password
  click_button "signin-signin"
end

_hearder.html:

    <% if sign_in? %>
      <nav>
        <ul class="nav pull-right">
          <li><%= link_to user_name_or_email(current_user), current_user %></li>
          <li><%= link_to "Users", users_path %></li>
          <li><%= link_to "Sign out", signout_path, method: "delete" %></li>
        </ul>
      </nav>
    <% else %>
      <%= form_for(:session, url: '/signinup', html: {class: "navbar-form pull-right"}) do |f| %>
        <%= f.email_field :email, id: "email", placeholder: "email", class: "span2"  %>
        <%= f.password_field :password, id: "Password", placeholder: "password", class: "span2" %>
        <%= f.submit "Sing-in/up", class: "btn btn-primary" %>
      <% end %>
    <% end %>

路线.rb:

match '/signin',  to: 'sessions#new'          , via: [:get, :post]

登录查看代码:

<% provide(:title, 'Sign in') %>
<h1>Sign in</h1>

<div class="row">
  <div class="span6 offset3">
    <%= form_for(:session, url: sessions_path) do |f| %>
      <%= f.label :email, "Email"%>
      <%=  f.text_field :email, id: "Email-signin"  %>

      <%= f.label :password %>
      <%= f.password_field :password, id: "Password-signin" %>

      <%= f.submit "Sign in", id: "signin-signin", class: "btn btn-large btn-primary" %>
    <% end %>

    Not a member? <%= link_to "Sign up now!", signup_path %>
  </div>
</div>

登录控制器代码:

  def create
    user = User.find_by_email(params[:session][:email])
    if user && user.authenticate(params[:session][:password])
      sign_in user
      redirect_back_or user
    else
      flash.now[:error] = "Invalid password/email combination"
      render 'new'
    end
  end

登录助手代码:

  def sign_in(user)
    cookies.permanent[:remember_token] = user.remember_token
    self.current_user = user
  end

  def sign_in?
    !current_user.nil?
  end

  def current_user=(user)
    @current_user = user
  end

  def current_user
    @current_user ||= User.find_by_remember_token(cookies[:remember_token])
  end

  def current_user?(user)
    user == current_user
  end

  def redirect_back_or(default)
    redirect_to(session[:return_to] || default)
    session.delete(:return_to)
  end

宝石:

source 'https://rubygems.org'

gem 'rails', '4.0.0'
gem 'bootstrap-sass', '2.3.2.0'
gem 'bcrypt-ruby', '3.0.1'
gem 'faker', '1.1.2'
gem 'will_paginate', '3.0.4'
gem 'bootstrap-will_paginate', '0.0.9'
gem 'jquery-rails', '3.0.1'
gem 'mysql2'
#gem 'execjs'
gem 'therubyracer', :platforms => :ruby

# rails 4 new
gem 'protected_attributes'        # https://github.com/rails/protected_attributes
gem 'activeresource'             # https://github.com/rails/activeresource
gem 'actionpack-action_caching'   # https://github.com/rails/actionpack-action_caching
gem 'activerecord-session_store'  # https://github.com/rails/activerecord-session_store
gem 'rails-observers'             # https://github.com/rails/rails-observers


# Dev and test gems
group :development, :test do
  gem 'rspec-rails', '2.13.2'
  gem 'guard-rspec', '3.0.2'
  gem 'guard-spork', '1.5.1'
  gem 'spork', '0.9.2'
end

# Gems used only for assets and not required
# in production environments by default.
# group :assets do
  gem 'sass-rails',   '4.0.0'
  gem 'coffee-rails', '4.0.0'
  gem 'uglifier', '2.1.1'
# end

#only test gems
group :test do
  #gem 'capybara', '2.0.2'
  gem 'capybara', '2.1.0'
  gem 'factory_girl_rails', '4.2.1'
  gem 'cucumber-rails', '1.3.0', :require => false
  gem 'database_cleaner', '1.0.1'
  gem 'launchy', '2.1.0'
  # gem 'rb-fsevent', '0.9.1', :require => false
  # gem 'growl', '1.0.3'
end
4

2 回答 2

0

untilities.rb

defdef sign_in(user) #this might be a problem (defdef)
  visit signin_path
  fill_in "Email-signin",     with: user.email
  fill_in "Password-signin",  with: user.password
  click_button "signin-signin"
end
于 2013-07-02T12:56:48.623 回答
0

我将方法名称更改utilities.rbsign_in_utl(并在规范中相应更改)并解决了问题。规范访问的sign_in是 session_helper 中的方法,而不是 utility.rbsign_in中的方法。在 ruby​​ 3 中,我没有遇到这个问题,因为规范没有看到助手。如果您知道修复它的方法(告诉规范不要查看助手) - 发表评论。

于 2013-07-13T21:18:48.333 回答