2014-05-15 2 views
1

컨트롤러를 테스트하려고합니다. update 작업을 테스트 할 때까지 모든 작업이 제대로 진행되고있었습니다.minitest를 사용하여 Rails에서 컨트롤러의 업데이트 메소드를 테스트하는 방법은 무엇입니까?

books GET /books(.:format)      books#index 
      POST /books(.:format)      books#create 
new_book GET /books/new(.:format)     books#new 
edit_book GET /books/:id/edit(.:format)    books#edit 
    book GET /books/:id(.:format)     books#show 
      PATCH /books/:id(.:format)     books#update 
      PUT /books/:id(.:format)     books#update 
      DELETE /books/:id(.:format)     books#destroy 

그리고 때 실행 :

내 시험

require 'test_helper' 

class BooksControllerTest < ActionController::TestCase 
    test "should not update a book without any parameter" do 
     assert_raises ActionController::ParameterMissing do 
      put :update, nil, session_dummy 
     end 
    end 
end 

이것은 내 컨트롤러

class BooksController < ApplicationController 

    (...) 

    def update 
     params = book_params 
     @book = Book.find(params[:id]) 

     if @book.update(params) 
      redirect_to @book 
     else 
      render 'edit' 
     end 
    end 

    (...) 

    def book_params 
     params.require(:book).permit(:url, :title, :price_initial, :price_current, :isbn, :bought, :read, :author, :user_id) 
    end 
end 

책 컨트롤러 내 응용 프로그램의 경로는 다음이다 rake test 다음과 같이 표시됩니다.

1) Failure: 
BooksControllerTest#test_should_not_update_a_book_without_any_parameter [/Users/acavalca/Sites/book-list/test/controllers/books_controller_test.rb:69]: 
[ActionController::ParameterMissing] exception expected, not 
Class: <ActionController::UrlGenerationError> 
Message: <"No route matches {:action=>\"update\", :controller=>\"books\"}"> 
---Backtrace--- 
test/controllers/books_controller_test.rb:70:in `block (2 levels) in <class:BooksControllerTest>' 
test/controllers/books_controller_test.rb:69:in `block in <class:BooksControllerTest>' 
--------------- 

그래서 나는 무엇을 놓치고 있습니까? 나는 이것에 대한 검색을했지만 아무 것도 찾을 수 없었다. 내가 한 짓과 아주 비슷한 몇 가지 RSpec 예제 만 여전히 단서가 없다.

답변

4

적어도 Book의 ID를 보내야합니다. 경로는 다음과 같습니다 알 수 있습니다 : :id 부분은 URL의 필수적인 부분입니다

PUT /books/:id(.:format)     books#update 

있다. 즉, PUT/books/으로 변경하는 것은 의미가 없지만 ID35가 데이터베이스의 어떤 레코드와도 일치하지 않는 경우에도 /books/1 중 하나를 수행하는 것이 유효한 URL입니다.

적어도이 테스트를 수행하려면 :id에 대한 매개 변수를 보내야합니다.

+0

Duh! 고마워! :) –

+0

괜찮습니다! 이 응답을 응답으로 표시하십시오! – MrDanA