RSpec / Capybara没有“访问”Rails4应用程序

时间:2013-11-06 21:42:29

标签: ruby-on-rails ruby rspec ruby-on-rails-4 capybara

我有一个相当简单的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吐出更多有关正在发生的事情的信息?

这是我的宝石文件,如果有帮助:

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 visit method is not working根本没有任何帮助。

谢谢!

更新

产品控制器:

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位于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

....在发布所有这些之后,我想出来了......我在下面粘贴答案。很高兴彼得戈德斯坦提到“过滤器”,因为这就是问题所在......

1 个答案:

答案 0 :(得分:0)

找到解决方案:

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

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

我的坏。

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