0

我有一个相当简单的 rails 应用程序,基于 railstutorial.org 教程的前半部分。我有点头疼... Capybara 似乎没有访问我要访问的页面。我想确保用户在看到products#index操作之前已登录。我有“未登录”测试通过,但“登录”测试在应该通过时仍然失败。

这是我的产品页面规范:

require 'spec_helper'

describe "Product pages" do

  subject { page }

  describe "index" do

    context "when not signed in" do
      before { visit products_path }

      it { should have_title 'Sign in' }
    end

    context "when signed in" do

      let(:user) { FactoryGirl.create(:user) }

      before do
        sign_in user
        visit products_path
      end

      it { should have_title('Listing all products') }

    end
  end 
end

第一个测试通过,但第二个没有。使用一些试验和错误我能够弄清楚它会进入主页(如果我把主页的标题放在should have_title()它通过)

我对 RSpec 很陌生,但是几乎相同的 user_pages_spec 测试工作正常:

require 'spec_helper'

describe "User pages" do

  subject { page }

  describe "index" do

    let(:user) { FactoryGirl.create(:user) }

    before do
      sign_in user
      visit users_path
    end

    it { should have_title('All users') }
    it { should have_content('All users') }
  end
end

是什么导致一个失败而一个通过?有没有办法让 rspec 吐出更多关于正在发生的事情的信息?

这是我的gemfile,如果有帮助的话:

source 'https://rubygems.org'
ruby '2.0.0'
#ruby-gemset=micmanager

gem 'rails', '4.0.1'
gem 'pg', '0.15.1'
gem 'bootstrap-sass-rails'
gem 'bcrypt-ruby', '3.1.2'
gem 'faker', '1.1.2'
gem 'will_paginate', '3.0.4'
gem 'bootstrap-will_paginate', '0.0.9' 

group :development, :test do
  gem 'rspec-rails', '2.13.1'
  gem 'guard-rspec', '2.5.0'
  gem 'spork-rails', github: 'sporkrb/spork-rails'
  gem 'guard-spork', '1.5.0'
  gem 'childprocess', '0.3.6'
  gem 'guard-livereload', require: false
  gem 'rack-livereload'
  gem 'rb-fsevent', require: false
  gem 'factory_girl_rails', '4.2.1'
end

group :test do
  gem 'selenium-webdriver', '2.35.1'
  gem 'capybara', '2.1.0'
  gem 'growl', '1.0.3'
end

gem 'sass-rails', '4.0.0'
gem 'uglifier', '2.1.1'
gem 'coffee-rails', '4.0.0'
gem 'jquery-rails', '3.0.4'
gem 'turbolinks', '1.1.1'
gem 'jbuilder', '1.0.2'

group :doc do
  gem 'sdoc', '0.3.20', require: false
end

group :production do
  gem 'rails_12factor', '0.0.2'
end

任何帮助是极大的赞赏!我发现的唯一这样的问题是:Capybara 访问方法不起作用,这根本没有任何帮助。

谢谢!

更新

产品控制器:

class ProductsController < ApplicationController
  before_action :set_product, only: [:show, :edit, :update, :destroy]
  before_action :signed_in_user
  before_action :admin_user, only: [:index, :new, :edit, :update, :destroy]
  # GET /products
  # GET /products.json
  def index
    @products = Product.all
  end
end

before_action提到的s 在sessions_helper文件中:

    module SessionsHelper

  def sign_in(user)
    remember_token = User.new_remember_token
    cookies.permanent[:remember_token] = remember_token
    user.update_attribute(:remember_token, User.encrypt(remember_token))
    self.current_user = user
  end

  def signed_in?
    !current_user.nil?
  end

  def sign_out
    self.current_user = nil
    cookies.delete(:remember_token)
  end

  def current_user=(user)
    @current_user = user
  end

  def current_user
    remember_token = User.encrypt(cookies[:remember_token])
    @current_user ||= User.find_by(remember_token: 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

  def store_location
    session[:return_to] = request.url if request.get?
  end

  def signed_in_user
    unless signed_in?
      store_location
      redirect_to signin_url, notice: "Please sign in."
    end
  end

  def correct_user
    @user = User.find(params[:id])
    redirect_to root_url unless current_user?(@user)
  end

  def admin_user
    redirect_to root_url unless current_user.admin?
  end
end

....发布所有这些后,我想通了...我将答案粘贴在下面。彼得·戈德斯坦(Peter Goldstein)提到了“过滤器”,因为这就是问题所在……

4

1 回答 1

0

找到了解决方案:

before_action :admin_user, only: [:index, :new, :edit, :update, :destroy]应该是因为before_action :admin_user, only: [:new, :edit, :update, :destroy]我不想将索引操作限制为仅管理员,而是任何登录用户。(最初我确实想这样做,然后改变了主意)

所以它失败:admin_user并被重定向到主页,因此我的错误。

我的错。

我们学到了什么?孩子们,检查你的过滤器。始终检查您的过滤器。

于 2013-11-07T00:10:57.943 回答