【发布时间】:2013-12-10 05:16:32
【问题描述】:
所以我正在编写一些返回和处理 json 的 API 控制器。一个特别是会话控制器。我这样设置路线:
注意:(我只是向您展示了销毁方法,因为这是我唯一遇到的问题......)
namespace :api do
namespace :v1 do
resource :users
resources :sessions
end
end
然后我有如下路线:
api_v1_sessions GET /api/v1/sessions(.:format) api/v1/sessions#index
POST /api/v1/sessions(.:format) api/v1/sessions#create
new_api_v1_session GET /api/v1/sessions/new(.:format) api/v1/sessions#new
edit_api_v1_session GET /api/v1/sessions/:id/edit(.:format) api/v1/sessions#edit
api_v1_session GET /api/v1/sessions/:id(.:format) api/v1/sessions#show
PATCH /api/v1/sessions/:id(.:format) api/v1/sessions#update
PUT /api/v1/sessions/:id(.:format) api/v1/sessions#update
DELETE /api/v1/sessions/:id(.:format) api/v1/sessions#destroy
最后我为api/v1/sessions#destroy销毁了如下所示的代码:
def destroy
@user = User.find_by auth_token: param[:auth_token]
return invalid_user_crendentials unless @user
cookies.delete(@user.auth_token)
render json: { user_id: @user.id }, status: 200
end
然后我为此特定操作编写了以下规范测试:
context "Destroy a session" do
it "should NOT destroy a session" do
post :destroy
response.response_code.should == 401
end
it "should NOT destroy a session with invalid token" do
post :destroy, auth_token: '34534534'
response.response_code.should == 401
end
it "should destroy a session based on valid data" do
post :destory, auth_token: @user.auth_token
json = JSON.parse response.body
json.to_json.should have_json_path('user_id')
response.response_code.should == 200
end
end
但是当我运行它时,我得到:
No route matches {:auth_token=>"34534534", :controller=>"api/v1/sessions", :action=>"destroy"}
No route matches {:auth_token=>"QfEg4oa0UdhJF4K1VIBADg", :controller=>"api/v1/sessions", :action=>"destory"}
No route matches {:controller=>"api/v1/sessions", :action=>"destroy"}
没有路由怎么匹配?有吗?
【问题讨论】:
-
您是否在使用 Webmock 或 VCR 之类的东西在测试中排除请求?
-
FactoryGirl 是我用来创建用户的
标签: ruby-on-rails rspec routes