2013-07-11 6 views
1

내 테스트에 어떤 문제가 있는지 이해하는 데 어려움을 겪고 있지만 컨트롤러에 대한 업데이트 방법을 테스트 할 때 라우팅 일치가 계속 발생합니다. 브라우저를 통해 양식을 제출하면 작동합니다.레일 4 컨트롤러 테스트

내 경로 파일 :

namespace :merchant do 
    resources :users 
    get '/signup', to: "users#new" 
end 

내 컨트롤러 :

def update 
    respond_to do |format| 
    if @merchant_user.update(user_params) 
     format.html { redirect_to @merchant_user, notice: 'User was successfully updated.' } 
     format.json { head :no_content } 
    else 
     format.html { render action: 'show' } 
     format.json { render json: @merchant_user.errors, status: :unprocessable_entity } 
    end 
    end 
end 

내 시험 :

test "should update user" do 
    user = users(:jon) 
    user.first_name="Jonas" 
    put :update, :merchant_user =>user.attributes 
    assert_response :success 

결과 :

1) Error: 
Merchant::UsersControllerTest#test_should_update_user: 
ActionController::UrlGenerationError: No route matches {:merchant_user=> {"id"=>"846114006", "email"=>"[email protected]", "first_name"=>"Jonas", "last_name"=>"Sims", "password_digest"=>"$2a$10$LVbV7pkd7li8sobYEauoS.4JVA2ZHzAXgPFbyiojYqgcDBHUE9bXW", "type"=>"Merchant::User", "created_at"=>"2013-07-11 22:59:41 UTC", "updated_at"=>"2013-07-11 22:59:41 UTC"}, :controller=>"merchant/users", :action=>"update"} 
test/controllers/merchant/users_controller_test.rb:45:in `block in <class:UsersControllerTest>' 

힌트가 있습니까?

답변

2

경로 모양에서 id 매개 변수가 예상되는 것처럼 보입니다. 명령 줄에서 rake routes을 할 경우

은 아마이 put :update, id: user.id, merchant_user: user.attributes 같은 id에 전달하면 작동한다

/merchant/users/:id/update

과 같은 경로를 표시합니다.

+0

나중에 테스트 하겠지만, 초보자가 Rails에서 다시 픽업 할 때 문제가되는 것 같습니다 :). –

+0

그건 그랬어. Mohamad와 Leo Correa에게 감사드립니다. 나에게 도움이되었지만 레오의 대답을 받아 들일 것입니다. –

3

테스트가 올바르게 실행 되려면 사용자의 id을 전달해야합니다. 라우터가 resource/:id을 기대하기 때문에

put :update, id: user.id, merchant_user: {} 

당신이 오류를보고있다, 그러나 당신은 ID를 전달되지 않습니다

이보십시오.

Users#update은 id가 필요한 회원 작업입니다. 라우터는 사용자 ID 매개 변수 : /users/:id/update을 예상합니다. id가 없다면 메소드는 업데이트하려는 사용자를 찾는 방법이 없습니다.

관련 문제