6

私は現在MichaelHartlのRailsチュートリアルに従っており、大きな問題もなく7.22に到達しました。しかし、私は次のようなテストからの出力に困惑しています。

Failures:

  1) UserPages signup with invalid information should not create a user
     Failure/Error: expect{click_button submit }.not_to change(User, :count)
     AbstractController::ActionNotFound:
       The action 'create' could not be found for UsersController
     # (eval):2:in `click_button'
     # ./spec/requests/user_pages_spec.rb:29:in `block (5 levels) in <top (required)>'
     # ./spec/requests/user_pages_spec.rb:29:in `block (4 levels) in <top (required)>'

  2) UserPages signup with valid information should create a user
     Failure/Error: expect{click_button submit}.to change(User, :count).by(1)
     AbstractController::ActionNotFound:
       The action 'create' could not be found for UsersController
     # (eval):2:in `click_button'
     # ./spec/requests/user_pages_spec.rb:42:in `block (5 levels) in <top (required)>'
     # ./spec/requests/user_pages_spec.rb:42:in `block (4 levels) in <top (required)>'

Finished in 0.7718 seconds
6 examples, 2 failures

チュートリアルの指示に従って、ユーザーコントローラーページに以下を追加しました。

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

  def new
    @user = User.new
  end

end

しかし、それでも機能しないようです。createメソッドを追加しようとしましたが、テンプレートが見つからないというエラーが返されます...

これが役立つ場合は、rakeroutesコマンドの出力を次に示します。

~/dev/rails/sample_app$ rake routes
    users GET    /users(.:format)          users#index
          POST   /users(.:format)          users#create
 new_user GET    /users/new(.:format)      users#new
edit_user GET    /users/:id/edit(.:format) users#edit
     user GET    /users/:id(.:format)      users#show
          PUT    /users/:id(.:format)      users#update
          DELETE /users/:id(.:format)      users#destroy
     root        /                         static_pages#home
   signup        /signup(.:format)         users#new
     help        /help(.:format)           static_pages#help
    about        /about(.:format)          static_pages#about
  contact        /contact(.:format)        static_pages#contact

コメントに応じて、失敗しているテストは次のとおりです。

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

アドバイスをよろしくお願いします!

4

7 に答える 7

20

その時点でテストに合格するべきではありません。チュートリアルを続けていくと、それがどのように機能するかがわかります。

于 2012-07-03T20:01:21.713 に答える
4

エラーコードを調べてここに直接来る前に、私は同じことで約30分苦労しました。@mhartlは明らかに正しく、チュートリアルにタイプミスはありません。「サインアップページのテスト」が再び機能するはずだと彼が言ったとき、それは少し混乱しているだけです。彼は、サインアップページからの提出については何も言及していません(チュートリアルのこの時点で失敗している2つのテストであるはずです)。

于 2013-03-06T05:05:03.747 に答える
1

私は同じ問題を抱えていました(両方のサインアップテストは7.3.1の終わりにまだ合格していませんでした)、後で私が次の間違いをしたことに気づきました:

createコントローラにメソッドを追加したときに、誤ってメソッドをUsers上書きしてしまいました(混乱しました)。ただし、両方の方法が必要です。今では動作します。newcreate

これが誰かの助けになることを願っています!

于 2012-09-20T20:02:59.757 に答える
0

config.use_transactional_fixtures=applications.rbファイルでtrue

ユーザーコントローラーで作成アクションが定義されていることを確認してください

于 2012-06-04T16:13:42.767 に答える
0

これをapp/controller/users_controller.rbに追加します。

class UsersController < ApplicationController
  attr_accessible :tags_attributes
  # GET /users
  # GET /users.json
  def index
    @users = User.all

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @users }
    end
  end

  # GET /users/1
  # GET /users/1.json
  def show
    @user = User.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @user }
    end
  end

  # GET /users/new
  # GET /users/new.json
  def new
    @user = User.new

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @user }
    end
  end

  # GET /users/1/edit
  def edit
    @user = User.find(params[:id])
  end

  # POST /users
  # POST /users.json
  def create
    @user = User.new(params[:user])

    respond_to do |format|
      if @user.save

        format.html { redirect_to @user, notice: 'User was successfully created.' }
        format.json { render json: @user, status: :created, location: @user }
      else
        format.html { render action: "new" }
        format.json { render json: @user.errors, status: :unprocessable_entity }
      end
    end
  end

  # PUT /users/1
  # PUT /users/1.json
  def update
    @user = User.find(params[:id])

    respond_to do |format|
      if @user.update_attributes(params[:user])
        format.html { redirect_to @user, notice: 'User was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: "edit" }
        format.json { render json: @user.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /users/1
  # DELETE /users/1.json
  def destroy
    @user = User.find(params[:id])
    @user.destroy

    respond_to do |format|
      format.html { redirect_to users_url }
      format.json { head :no_content }
    end
  end
end
于 2012-11-06T10:03:21.723 に答える
0

アクションを追加する必要がありますcreate(私は同じ問題を抱えていました):

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
      flash[:success] = "Welcome to the Sample App!"
      redirect_to @user
    else
      render 'new'
    end
  end
end
于 2013-02-03T16:35:40.720 に答える
0

同じ問題がありました。修正は、第2版の305ページの「サインアップの失敗」セクションに記載されているので、続行してください。コントローラにcreate-actionがありません。上で述べたように、サインアップページのテスト作業についての言及は混乱を招きます。フォームを送信するのではなく、機能するページだけです。

この本は絶対に素晴らしく、私が読んだ中で最高のコーディング本の1つであり、実際には最高です。ただし、多くのページまたはこの場合は本全体にまたがるサンプルコードの場合と同様に、特に何かを見逃した場合は、少し混乱する可能性があります。ここでの解決策は、本の中で、何がどの段階で機能するのか、何が機能しないのか、そして何が機能するように後で何が行われるのかを常に明確に述べることです。Hartlはそうしますが、いくつかのスポットはまだ人々を混乱させる可能性があります。繰り返して綴りを変えることは害にはなりません。

于 2015-08-08T16:43:02.653 に答える