0

我一直在研究 M. Hartl 的 Rails 教程,并且已经到了无法理解为什么我的身份验证测试无法通过的地步。我的应用程序在此时应该在各个方面工作,我想知道为什么测试继续失败。

我对 Rails 还是很陌生,如果我可以发布任何其他元素来帮助使情况更清楚,请告诉我。

斯图尔特

错误信息:

Failures:

1) Authentication signin with valid information 
 Failure/Error: it { should have_selector('title', text: user.name) }
   expected css "title" with text "Stuart Culpepper" to return something
 # ./spec/requests/authentication_pages_spec.rb:33:in `block (4 levels) in <top (required)>'

2) Authentication signin with valid information 
 Failure/Error: it { should have_link('Settings', href: edit_user_path(user)) }
   expected link "Settings" to return something
 # ./spec/requests/authentication_pages_spec.rb:35:in `block (4 levels) in <top (required)>'

3) Authentication signin with valid information 
 Failure/Error: it { should have_link('Profile', href: user_path(user)) }
   expected link "Profile" to return something
 # ./spec/requests/authentication_pages_spec.rb:34:in `block (4 levels) in <top (required)>'

4) Authentication signin with valid information 
 Failure/Error: it { should_not have_link('Sign in', href: signin_path) }
   expected link "Sign in" not to return anything
 # ./spec/requests/authentication_pages_spec.rb:37:in `block (4 levels) in <top (required)>'

5) Authentication signin with valid information 
 Failure/Error: it { should have_link('Sign out', href: signout_path) }
   expected link "Sign out" to return something
 # ./spec/requests/authentication_pages_spec.rb:36:in `block (4 levels) in <top (required)>'

我的身份验证 Rspec:

require 'spec_helper'

describe "Authentication" do

  subject { page }

  describe "signin page" do
before { visit signin_path }

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

  describe "signin" do
before { visit signin_path }

  describe "with invalid information" do
    before { click_button "Sign in" }

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

    describe "after visiting another page" do
        before { click_link "Home" }
        it { should_not have_error_message( 'invalid')}
  end
 end

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

    it { should have_selector('title', text: user.name) }
    it { should have_link('Profile', href: user_path(user)) }
    it { should have_link('Settings', href: edit_user_path(user)) }
    it { should have_link('Sign out', href: signout_path) }
    it { should_not have_link('Sign in', href: signin_path) }

  describe "followed by signout" 
    before { click_link "Sign out" }
    it { should have_link('Sign in') }
  end
end

end

我的 Rrpec/Support/Utilities.rb

include ApplicationHelper

def valid_signin(user)
fill_in "Email",    with: user.email
fill_in "Password", with: user.password
click_button "Sign in"
end

RSpec::Matchers.define :have_error_message do |message|
match do |page|
    page.should have_selector('div.alert.alert-error', text: message)
end
end

def sign_in(user)
  visit signin_path
  fill_in "Email",    with: user.email
  fill_in "Password", with: user.password
  click_button "Sign in"
  # Sign in when not using Capybara as well.
  cookies[:remember_token] = user.remember_token
end

会话控制器

class SessionsController < ApplicationController

def new
    @title = "Sign in"
end

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

def destroy
    sign_out
    redirect_to root_url
end
end

路由.rb

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

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

  root to: 'static_pages#home'

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

应用程序/视图/会话/new.html.erb

<% 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 %>
            <%= f.text_field :email %>

            <%= f.label :password %>
            <%= f.text_field :password %>

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

        <p>New user? <%= link_to "Sign up now!", signup_path %><p>
    </div>
</div>

app/spec/requests/user_pages_spec.rb

require 'spec_helper'

describe "User pages" do

  subject { page }

  describe "profile page" do
    let(:user) { FactoryGirl.create(:user) }
    before { visit user_path(user) }

    it { should have_selector('h1',    text: user.name) }
    it { should have_selector('title', text: user.name) }
  end

  describe "signup page" do
    before { visit signup_path }

    it { should have_selector('h1',    text: 'Sign up') }
    it { should have_selector('title', text: full_title('Sign up')) }
  end

describe "signup" do

    before { visit signup_path }

    let(:submit) { "Create my account" }

    describe "with invalid information" do
      it "should not create a user" do
        expect { click_button submit }.not_to change(User, :count)
      end
    end

    describe "after submission" do
        before { click_button submit }

        it { should have_selector('title', text: 'Sign up') }
        it { should have_content('error') }
    end

    describe "with valid information" do
      before do
        fill_in "Name",         with: "Example User"
        fill_in "Email",        with: "user@example.com"
        fill_in "Password",     with: "foobar"
        fill_in "Confirmation", with: "foobar"
      end

      describe "after saving the user" do
        before { click_button submit }
        let(:user) { User.find_by_email('user@example.com') }

        it { should have_selector('title', text: user.name) }
        it { should have_selector('div.alert.alert-success', text: 'Welcome') }
        it { should have_link('Sign out') }
      end

      it "should create a user" do
        expect { click_button submit }.to change(User, :count).by(1)
      end
    end
  end

  describe "edit" do
  let(:user) { FactoryGirl.create(:user) }
    before { visit edit_user_path(user) }

  describe "page" do
    it { should have_selector( 'h1', text: "Update your profile" ) }
    it { should have_error_message('title', text: "Edit user") }
    it { should have_link( 'change', href: 'http://gravatar.com/emails') }
  end

    describe "with invalid information" do
      before { click_button "Save changes" }

      it { should have_content( 'error' ) }
  end
 end
end
4

0 回答 0