0

I am trying to work through the Rails tutorial at railstutorial.org and am stuck at this problem. My user_path in the test is supposed to go to the individual user's profile page, but is instead going to users/index (which I haven't created yet, on purpose).

/* My test code */

  describe "profile page" do
    #let(user) { FactoryGirl.create(:user) }
    @user = User.new(name: "Example User", email: "user@example.com",
                    password: "foobar", password_confirmation: "foobar")
    before { visit user_path(@user) }

    it { should have_content(user.name) }
    it { shoult have_title(user.name)   }

  end

/* Failures: */

 1) UserPages profile page
    ←[31mFailure/Error:←[0m ←[31mbefore { visit user_path(@user) }←[0m
    ←[31mActionView::MissingTemplate←[0m:
      ←[31mMissing template users/index, application/index with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :raw, :ruby, :jbuilder, :coffe
}. Searched in:←[0m
      ←[31m  * "C:/Users/rajeshch/Documents/Projects/sample_app/app/views"←[0m
[36m     # C:in `find_template'←[0m
[36m     # ./spec/requests/user_pages_spec.rb:19:in `block (3 levels) in <top (required)>'←[0m

The routes file shows that user_path should give me users/{id} or so

> rake routes
   Prefix Verb   URI Pattern               Controller#Action
    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      <<<------
          PATCH  /users/:id(.:format)      users#update
          PUT    /users/:id(.:format)      users#update
          DELETE /users/:id(.:format)      users#destroy
     root GET    /                         static_pages#home
   signup GET    /signup(.:format)         users#new
     help GET    /help(.:format)           static_pages#help
    about GET    /about(.:format)          static_pages#about
  contact GET    /contact(.:format)        static_pages#contact

/* users_controller.rb*/

class UsersController < ApplicationController

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

    def new
    end

end

/* config/routes.rb */

SampleApp::Application.routes.draw do

  resources :users
  root 'static_pages#home'

  match '/signup',  to: 'users#new',            via: 'get'
  match '/help',    to: 'static_pages#help',    via: 'get'
  match '/about',   to: 'static_pages#about',   via: 'get'
  match '/contact', to: 'static_pages#contact', via: 'get'

end
4

3 回答 3

3

您应该先使用@user.save 保存用户!在调用之前访问 user_path(@user)

因为在您保存用户之前,它不会保留在数据库中并且不会有任何 id。

describe "profile page" do
  #let(user) { FactoryGirl.create(:user) }
  @user = User.new(name: "Example User", email: "user@example.com",
                password: "foobar", password_confirmation: "foobar")

 # Add this to your code ..
  @user.save! 

  before { visit user_path(@user) }

  it { should have_content(user.name) }
  it { shoult have_title(user.name)   }

end
于 2013-09-13T16:53:00.253 回答
1

你检查过是否@user有价值吗?

user_path(@user)会去,/users/<@user.id>但如果@user 为零,它会尝试去/users/。鉴于您尚未创建索引方法和视图,这可能就是它失败的原因。

感谢您的测试代码。首先,您的用户不在数据库中 - 更改.new.create或添加@user.save. 其次,您params[:id]没有在测试代码中填充,因此查找将失败(添加controller.params[:id] = @user.id到您的测试中)。(第三,你有一个错字shoult,而不是should在最后一次测试中。)

于 2013-09-13T14:14:13.880 回答
0

您似乎没有包含您的测试代码,这对解决这个问题非常有帮助。最有可能的是,您在测试中将 @user 设置为 nil,修复它可以解决问题。

于 2013-09-13T14:14:56.053 回答