我有一些看起来像这样的路线:
constraints ClientCodeConstraint.new do
get 'share/:client_code', to: redirect('/%{client_code}')
get 'share/:client_code/:id', to: redirect('/%{client_code}/%{id}')
get ':client_code', to: 'share#index', as: :shares
get ':client_code/:id', to: 'share#show', as: :share
get ':client_code/:document_id/more/:component_id', to: 'share#more', as: :more
end
我的约束如下:
class ClientCodeConstraint
def matches?(request)
users.each do |user|
return true if request.path_parameters[:client_code] == user.client_code
end
end
def users
@users ||= User.all
end
end
我有这样的测试,但我没有运气:
context 'not logged in' do
let!(:user) { create(:user) }
let!(:document) { create(:document) }
specify 'renders the :index view' do
get :index
expect(response.code).to eq '302'
expect(response).to redirect_to("/#{assigns(:user).client_code}")
end
specify 'renders the :show view' do
get :show
expect(response).to redirect_to("/#{assigns(:user).client_code}/#{assigns(:document).id}")
end
end
如何使用Rspec测试这些重定向?我只是得到了一堆这些错误:
ActionController::RoutingError: No route matches {:controller=>"share"}
当然存在控制器和路线。
更新
相关的rake routes
输出在这里:
GET /share/:client_code(.:format) :controller#:action
GET /share/:client_code/:id(.:format) :controller#:action
shares GET /:client_code(.:format) share#index
share GET /:client_code/:id(.:format) share#show
more GET /:client_code/:document_id/more/:component_id(.:format) share#more
测试通过如下:
specify 'renders the :index view' do
get :index, client_code: user.client_code
expect(response.status).to eq(200)
end
感觉就像我只测试响应是否正确。我想更彻底地测试,即它不会呈现错误消息等等。