0

Michael Hartl の貴重なチュートリアルを読んでいるのですが、いくつかの Rspec エラーに行き詰まりました。すべてを再確認しましたが、まだ何かが足りないようです。エラーメッセージは次のようになります。

エラー

私を最も悩ませているのは、Microposts モデルを生成しているときに、オプションの 1 つで誤ってタイプミスをしたため、モデルを再度生成する前に、モデル Microposts を破棄して生成コマンドを元に戻すことでした。それが私が見ているエラーと関係があるかどうか疑問に思っています。

このチュートリアルをできるだけ早く終了して、独自の Web アプリケーションの構築に取り掛かりたいと思っています。任意の助けをいただければ幸いです。

私のコードは次のようになります。

micropost_pages_spec.rb

require 'spec_helper'

describe "MicropostPages" do
    subject {page}
    let(:user) {FactoryGirl.create(:user)}
        before {sign_in user}
        describe "micropost creation" do
            before {visit root_path}

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

            describe "error messages" do
                before {click_button "Post"}
                it {should have_content('error')}
            end
        end
    end
end

microposts_controller.rb

class MicropostsController < ApplicationController
    before_filter :signed_in_user, only: [:create, :destroy]

    def create
        @micropost = current_user.micropost.build(params[:micropost])
        if @micropost.save
            flash[:success] = "Micropost created!"
            redirect_to root_path
        else
            render 'static_pages/home'
        end
    end

    def destroy
    end
end

static_pages_controller.rb

class StaticPagesController < ApplicationController
  def home
    @micropost = current_user.microposts.build if signed_in?
  end

  def help
  end

  def about
  end

  def contact
  end

end

user.rb (ユーザーモデル)

class User < ActiveRecord::Base
  attr_accessible :email, :name, :password, :password_confirmation
  has_secure_password
  has_many :microposts, dependent: :destroy

  before_save {self.email.downcase!}
  before_save :create_remember_token

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

  private

    def create_remember_token
      self.remember_token = SecureRandom.urlsafe_base64
    end
end
4

1 に答える 1

1

エラーは次の行にあります。

@micropost = current_user.micropost.build(params[:micropost])

そのはず:

@micropost = current_user.microposts.build(params[:micropost])

micropostを使用すべきときに使用していmicropostsます。

于 2013-02-04T01:18:45.747 に答える