简体   繁体   English

RSpec / Capybara无法在Rails4应用程序中“访问”

[英]RSpec / Capybara not 'visit'-ing in Rails4 app

I have a fairly simple rails app, based off of the first half of the railstutorial.org tutorial. 我有一个相当简单的rails应用程序,它基于railstutorial.org教程的前半部分。 I have a bit of head scratcher here... Capybara doesn't seem to be visiting the page i'm saying to visit. 我在这里有些挠头...水豚似乎没有访问我要访问的页面。 I want to make sure a user is logged in before they can see the products#index action. 我想确保用户已登录,然后才能看到products#index操作。 I have the 'not logged in' test passing fine, but the 'logged in' test keeps failing when it should be passing. 我的“未登录”测试通过了,但是“登录”测试在通过时仍然失败。

Here's my Product pages spec: 这是我的产品页面规格:

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

the first test passes, but the second one doesn't. 第一个测试通过了,但是第二个没有通过。 Using some trial and error i was able to figure out that it's going to the homepage (if i put the homepage's title in the should have_title() it passes) 通过一些试验和错误,我能够弄清楚它要进入主页(如果我将主页的标题放在它should have_title()通过的should have_title()

Im pretty new to RSpec, but the user_pages_spec test that is pretty much identical is working a-ok: 我对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

what could be causing one to fail and one to pass? 是什么导致一个人失败而一个人通过? is there a way to have rspec spit out more information as to what's happening? 有没有办法让rspec吐出有关正在发生的事情的更多信息?

here's my gemfile, if that helps: 这是我的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

Any help is greatly appreciated! 任何帮助是极大的赞赏! The only SO question i found like this was: Capybara visit method is not working which isn't any help at all. 我发现的唯一这样的问题是: 水豚探视方法不起作用 ,这根本没有帮助。

thanks! 谢谢!

UPDATES 更新

Products Controller: 产品负责人:

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

The before_action s that are mentioned, are in the sessions_helper file: 提到的before_actionsessions_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

....and after posting all this, i figured it out... i paste the answer below. ....在发布了所有这些内容之后,我就知道了...我在下面粘贴了答案。 big h/t to Peter Goldstein for mentioning 'filters' since that's where the issue was... 非常感谢Peter Goldstein提到“过滤器”,因为这就是问题所在。

Found the solution: 找到了解决方案:

the before_action :admin_user, only: [:index, :new, :edit, :update, :destroy] should haave been before_action :admin_user, only: [:new, :edit, :update, :destroy] since i don't want to restrict the index action to only admins but rather any logged in user. before_action :admin_user, only: [:index, :new, :edit, :update, :destroy]应该是before_action :admin_user, only: [:new, :edit, :update, :destroy]因为我没有希望将索引操作限制为仅管理员,而不是任何登录用户。 (originally i did want to do this, then changed my mind) (最初我确实想这样做,然后改变了主意)

so it was failing on :admin_user and getting redirected to the home page, thus my error. 所以它在:admin_user上失败:admin_user重定向到主页,因此是我的错误。

my bad. 我的错。

what did we learn? 我们学到了什么? Check your filters, kids. 检查您的过滤器,孩子们。 Always check your filters. 始终检查您的过滤器。

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

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