在rails中调试测试方法

时间:2016-07-07 22:03:29

标签: ruby unit-testing ruby-on-rails-4 minitest

我正在完成Michael Hartl的导轨教程,我被困在https://www.railstutorial.org/book/following_users#code-show_follow_view

def setup @user = users(:michael) @other_user = users(:archer) end test "should redirect following when not logged in" do get following_user_path(@user) assert_redirected_to login_url end 导致的错误:

      1) Error:
    UsersControllerTest#test_should_redirect_following_when_not_logged_in:
    ActionController::UrlGenerationError: No route matches {:action=>"/users/762146111/following", :controller=>"users"}
      test/controllers/users_controller_test.rb:62:in `block in <class:UsersControllerTest>'

错误讯息:

@user

设置中的test/fixtures/users.yml来自michael: name: Michael Example email: michael@example.com password_digest: <%= User.digest('password') %> admin: true activated: true activated_at: <%= Time.zone.now %>

routes.rb

resources :users do member do get :following, :followers end end

app/controllers/users_controller.rb

class UsersController < ApplicationController before_action :logged_in_user, only: [:index, :edit, :update, :destroy, :following, :followers] def following @title = "Following" @user = User.find(params[:id]) @users = @user.following.paginate(page: params[:page]) render 'show_follow' end end

rake routes

Prefix Verb URI Pattern Controller#Action password_resets_new GET /password_resets/new(.:format) password_resets#new password_resets_edit GET /password_resets/edit(.:format) password_resets#edit sessions_new GET /sessions/new(.:format) sessions#new users_new GET /users/new(.:format) users#new root GET / static_pages#home help GET /help(.:format) static_pages#help about GET /about(.:format) static_pages#about contact GET /contact(.:format) static_pages#contact signup GET /signup(.:format) users#new login GET /login(.:format) sessions#new POST /login(.:format) sessions#create logout DELETE /logout(.:format) sessions#destroy following_user GET /users/:id/following(.:format) users#following followers_user GET /users/:id/followers(.:format) users#followers 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 GET /users(.:format) users#index POST /users(.:format) users#create GET /users/new(.:format) users#new GET /users/:id/edit(.:format) users#edit GET /users/:id(.:format) users#show PATCH /users/:id(.:format) users#update PUT /users/:id(.:format) users#update DELETE /users/:id(.:format) users#destroy edit_account_activation GET /account_activations/:id/edit(.:format) account_activations#edit password_resets POST /password_resets(.:format) password_resets#create new_password_reset GET /password_resets/new(.:format) password_resets#new edit_password_reset GET /password_resets/:id/edit(.:format) password_resets#edit password_reset PATCH /password_resets/:id(.:format) password_resets#update PUT /password_resets/:id(.:format) password_resets#update microposts POST /microposts(.:format) microposts#create micropost DELETE /microposts/:id(.:format) microposts#destroy relationships POST /relationships(.:format) relationships#create relationship DELETE /relationships/:id(.:format) relationships#destroy

/users/762146111/following

我不确定为什么错误会显示测试试图点击的路线rake db:migrate:reset。 762146111是用户ID应该在哪里,我假设应该是1.我尝试rake db:seed后跟debugger,但id保持不变。我还尝试将sudo gem install ruby-debug添加到断言上方的方法中,但我在安装byebug时遇到问题。 <div class="col-sm-6 lakerlink" ng-class="{'has-error' : lakerLookup.dateofbirth.$error.required || lakerLookup.dateofbirth.$error.minlength , 'has-success' : !lakerLookup.dateofbirth.$invalid && !lakerLookup.dateofbirth.$dirty}" > <input id="dateOfBirth" type="text" name="dateofbirth" class="form-control laker-date-input" ng-minlength="8" ng-maxlength="10" placeholder="mm/dd/yyyy" datepicker-popup="MM/dd/y" ng-model="DateOfBirth" is-open="opened" datepicker-options="dateOptions" date-disabled="disabled(date, mode)" tabindex="2" required/> <span class="input-group-btn link-date-btn"> <button type="button" class="btn date-picker-btn" ng-click="open($event)" tabindex="4"><i class="glyphicon glyphicon-calendar"></i></button> </span> </div> 也只是在没有断点的情况下运行测试。

非常感谢任何见解或想法

2 个答案:

答案 0 :(得分:1)

您尚未设置处理&#34; users /:id / follow&#34;

的路线

您需要在routes.rb

中使用此功能
  resources :users do
    member do
      get :following, :followers
    end
  end

查看教程中的清单14.15。

奇怪的数字是test / fixtures / users.yml

中使用的数字

答案 1 :(得分:0)

我有同样的问题,但在第10章中有测试。在我的情况下,问题是我开始教程并使用旧版本的Rails生成Users控制器。当我浏览教程时,rails版本已经更新,因此Users控制器属于旧版本。

我的解决方案是销毁用户控制器,再次生成它并写下所有已经重新擦除的代码。现在它完美无缺。