6

我目前正在关注 Michael Hartl 的 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

但它似乎仍然不起作用。我试过添加一个创建方法,但这只会抛出一个丢失的模板错误......

如果它有帮助,这里是 rake routes 命令的输出:

~/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

在查看错误代码并直接来到这里之前,我在同一件事上挣扎了大约半个小时。@mhartl 显然是正确的,教程中没有错字,只是当他说“注册页面的测试”应该再次工作时有点令人困惑。他没有提到来自注册页面的提交(这应该是教程中此时失败的两个测试)。

于 2013-03-06T05:05:03.747 回答
1

我遇到了同样的问题(在 7.3.1 结束时两个注册测试仍然没有通过),后来注意到我犯了以下错误:

当我create在控制器中添加方法时Users,我不小心用方法覆盖了new方法create(我把它们弄糊涂了)。但是,您需要这两种方法。现在它起作用了。

我希望这对某人有帮助!

于 2012-09-20T20:02:59.757 回答
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

config.use_transactional_fixtures = true 在您的 applications.rb 文件中

并确保您在用户控制器中定义了创建操作

于 2012-06-04T16:13:42.767 回答
0

有同样的问题。修复出现在第二版第 305 页的“注册失败”部分,所以请继续。您缺少控制器中的创建操作。如上所述,注册页面测试工作的提及令人困惑。它只是有效的页面,而不是提交表单。

这本书绝对是一流的,是我读过的最好的编码书籍之一,实际上是最好的。然而,与跨越许多页面或在这种情况下是整本书的示例代码一样,它可能会有点混乱,特别是如果你错过了一些东西。这里的解决方案是始终在书中非常清楚地说明什么应该在什么阶段起作用,什么不是,以及稍后将要做哪些事情以使事情起作用。Hartl 确实如此,但有些地方仍然可能让人们感到困惑。重复和拼写出来不会受到伤害。

于 2015-08-08T16:43:02.653 回答