Глава 8. Учебник по Rails. Запоминание ошибок токенов

Эта глава посвящена добавлению маркеров запоминания, чтобы гарантировать, что статус входа пользователя запоминается и что сеанс очищается только тогда, когда пользователь явно выходит из системы. Я понимаю важность наличия этой функции в моем приложении, поэтому хочу убедиться, что она работает правильно. Однако я получаю кучу ошибок, когда запускаю

$ bundle exec rspec spec/

, и я подозреваю, что они связаны с моей пользовательской моделью, поскольку все они, кроме одной, содержат :

NoMethodError:
       undefined method `remember_token=' for #<User:...>

, а последняя содержит

Failure/Error: it { should respond_to(:remember_token) }

, а затем указывают на моего пользователя _spec.rb, user.rb и аутентификация _страницы _файлы spec.rb, большую часть (соответствующих частей )которых я включил сюда.

user.rb:

# == Schema Information
#
# Table name: users
#
#  id         :integer         not null, primary key
#  name       :string(255)
#  email      :string(255)
#  created_at :datetime        not null
#  updated_at :datetime        not null
#

class User < ActiveRecord::Base
  attr_accessible :name, :email, :password, :password_confirmation
  has_secure_password

  before_save { |user| user.email = email.downcase }
  before_save :create_remember_token

  validates :name, presence: true, length: { maximum: 50 }
  VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
  validates :email, presence:   true,
                    format:     { with: VALID_EMAIL_REGEX },
                    uniqueness: { case_sensitive: false }
  validates :password, length: { minimum: 6 }
  validates :password_confirmation, presence: true

  private

      def create_remember_token
        self.remember_token = SecureRandom.urlsafe_base64
      end
end

аутентификация _страницы _spec.rb:

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')}

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

    describe "with valid information" do
          let(:user) { FactoryGirl.create(:user) }
          before do
            fill_in "Email",    with: user.email
            fill_in "Password", with: user.password
            click_button "Sign in"
          end

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

и начало пользователя _spec.rb:

# == Schema Information
#
# Table name: users
#
#  id         :integer         not null, primary key
#  name       :string(255)
#  email      :string(255)
#  created_at :datetime        not null
#  updated_at :datetime        not null
#

require 'spec_helper'

describe User do

  before do
      @user = User.new(name: "Example User", email: "user@example.com", 
                       password: "foobar", password_confirmation: "foobar")
  end

  subject { @user }

  it { should respond_to(:name) }
  it { should respond_to(:email) }
  it { should be_valid }
  it { should respond_to(:password_digest) }
  it { should respond_to(:password) }
  it { should respond_to(:password_confirmation) }
  it { should respond_to(:authenticate) }
  it { should respond_to(:remember_token) }

  describe "remember token" do
      before { @user.save }
      its(:remember_token) { should_not be_blank }
    end
.
.
.

Будем признательны за любую помощь!

6
задан skirmishdirmish 29 April 2012 в 17:45
поделиться