【问题标题】:Rails tutorial Chapter 9.3 Failing testsRails 教程第 9.3 章失败的测试
【发布时间】:2013-04-19 07:50:05
【问题描述】:

我查看了通过问题,但找不到可以解决我的问题的问题。我得到了这些失败的测试。我什至通过浏览器访问了用户索引页面,并看到了登录标题。

Failures:

1) authorization for non-signed-in users in the Users controller visiting the edit page 
 Failure/Error: it { should have_selector('title', text: 'Sign in') }
   expected css "title" with text "Sign in" to return something
 # ./spec/requests/authentication_pages_spec.rb:75:in `block (5 levels) in <top (required)>'

2) authorization for non-signed-in users in the Users controller visiting the user index 
 Failure/Error: it { should have_selector('title', text: 'Sign in') }
   expected css "title" with text "Sign in" to return something
 # ./spec/requests/authentication_pages_spec.rb:85:in `block (5 levels) in <top (required)>'

Finished in 4.65 seconds
69 examples, 2 failures

Failed examples:

rspec ./spec/requests/authentication_pages_spec.rb:75 # authorization for non-signed-in 
users in the Users controller visiting the edit page 

rspec ./spec/requests/authentication_pages_spec.rb:85 # authorization for non-signed-in 
users in the Users controller visiting the user index 

Authentication_pages_spec.rb

describe "authorization" do

  describe "for non-signed-in users" do
    let(:user) { FactoryGirl.create(:user) }

    describe "when attempting to visit a protected page" do
      before do
        visit edit_user_path(user)
        fill_in "Email",    with: user.email
        fill_in "Password", with: user.password
        click_button "Sign in"
      end

      describe "after signing in" do

        it "should render the desired protected page" do
          page.should have_selector('title', text: 'Edit user')
        end
      end
    end


    describe "in the Users controller" do

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

      describe "visiting the edit page" do
        before { visit edit_user_path(user) }
        it { should have_selector('title', text: 'Sign in') }
      end

      describe "submitting to the update action" do
        before { put user_path(user) }
        specify { response.should redirect_to(signin_path) }
      end

      describe "visiting the user index" do
        before { visit users_path }
        it { should have_selector('title', text: 'Sign in') }
      end
    end

Routes.rb

SampleApp::Application.routes.draw do
resources :users
resources :sessions, only: [:new, :create, :destroy]

root to: 'static_pages#home'

match '/signup', to: 'users#new'
match '/signin', to: 'sessions#new'
match '/signout', to: 'sessions#destroy', via: :delete

match '/help', to: 'static_pages#help'
match '/about', to: 'static_pages#about'
match '/contact', to: 'static_pages#contact'

users_controllers.rb

    class UsersController < ApplicationController
      before_filter :signed_in_user, 
                    only: [:index, :edit, :update]
      before_filter :correct_user, only: [:edit, :update]

      def show
        @user = User.find(params[:id])
      end

      def new
        @user = User.new
      end

      def index
        @users = User.all
      end

      def edit
        @user = User.find(params[:id])
      end

      def update
        @user = User.find(params[:id])
        if @user.update_attributes(params[:user])
          flash[:success] = "Profile updated"
          sign_in @user
          redirect_to @user
        else
          render 'edit'
        end
      end

      def create
        @user = User.new(params[:user])
        if @user.save
          sign_in @user
          flash[:success] = "Welcome to the Sample App!"
          redirect_to @user
        else
          render 'new'
        end
      end

      private

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

提前致谢

【问题讨论】:

    标签: ruby-on-rails rspec railstutorial.org


    【解决方案1】:

    您的 Authentication_pages_spec.rb 中缺少以下代码:

    describe "authorization" do
    
      subject { page } # This line.
    
      describe "for non-signed-in users" do
        let(:user) { FactoryGirl.create(:user) }
        .
        .
        .
    

    请参阅http://ruby.railstutorial.org/chapters/filling-in-the-layout#sec-pretty_rspec

    或者,替换

    it { should have_selector('title', text: 'Sign in') }
    

    page.should have_selector('title', text: 'Sign in')
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多