0

我正在学习 Michael Hartls RoR 教程,这是我的第一次编码体验。

在过去的几天里,我因 rspec 失败而失去了一些头发 - 希望你们能提供帮助。

Mhartl,第 9 章 - 清单 9.3

bundle exec rspec spec/requests/user_pages_spec.rb -e "edit page" 运行选项:include {:full_description=>/edit\ page/}

    Failure:
    1) User pages edit page 
         Failure/Error: it { should have_selector('title', text: "Edit user") }
           expected css "title" with text "Edit user" to return something
         # ./spec/requests/user_pages_spec.rb:75:in `block (4 levels) in <top (required)>'

3 examples, 1 failure

Failed examples:

rspec ./spec/requests/user_pages_spec.rb:75 # User pages edit page 

用户页面规范:

 require 'spec_helper'

    describe "User pages" do

      subject { page }

      describe "signup page" do
        before { visit signup_path }

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

      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" 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

          describe "after submission" do
            before { click_button submit }

            it { should have_selector('title', text: 'Sign up') }
            it { should have_content('error') }
            it { should_not have_content('Password digest') }
          end
        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

          it "should create a user" do
            expect { click_button submit }.to change(User, :count).by(1)
          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
        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_selector('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

身份验证页面规范

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_selector('div.alert.alert-error', text: 'Invalid') }
    end

    describe "after visiting another page" do
        before { click_link "Home" }
        it { should_not have_selector('div.alert.alert-error') }
    end

    describe "with valid information" do
      let(:user) { FactoryGirl.create(:user) }
      before { sign_in 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" do
        before { click_link "Sign out" }
        it { should have_link('Sign in') }
      end
    end
  end
end

用户控制器

class UsersController < ApplicationController

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

  def new
    @user = User.new
  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

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

会话助手

module SessionsHelper

  def sign_in(user)
    cookies.permanent[:remember_token] = user.remember_token
    self.current_user = user
  end

  def signed_in?
    !current_user.nil?
  end

  def current_user=(user)
    @current_user = user
  end

  def current_user
    @current_user ||= User.find_by_remember_token(cookies[:remember_token])
  end

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

实用程序

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
4

4 回答 4

1

重启服务器后故障消失!

于 2012-12-27T21:09:36.150 回答
0

您确定它查询的页面实际上有标题吗?看起来它在 HTML 中找不到标题。检查您对该页面的视图,您应该在某处设置标题。titleRspec 失败看起来在 DOM 中找不到,nil而是返回。

于 2012-12-27T19:00:50.273 回答
0

尝试添加:

<% provide(:title, "Edit user") %>

到您的 app/views/users/edit.html.erb 文件的顶部。

于 2012-12-27T19:35:14.853 回答
0

此外,如果您使用它进行测试,请确保重新启动Spork 。我遇到了同样的问题。

于 2013-06-15T14:02:49.743 回答