我的Rails 4应用程序中有一个小型REST API。基本上,它是一个有7个动作的控制器。只有经过身份验证的用户才能访问API,并且检查被添加为Devise中的before_filter。最好的测试方法是什么?
我已经对API进行了单元测试,如果需要身份验证,可以手动测试每个操作,但我显然不喜欢这样做。
发布于 2015-12-09 21:02:50
在RSpec中您可以使用使用共享示例
# spec/support/shared_examples/authentication.rb
RSpec.shared_examples "an authenticated action" do |action|
it "requires authentication" do
expect { action.call }.to raise_exception("uncaught throw :warden")
end
endrequire 'rails_helper'
require 'support/shared_examples/authentication'
RSpec.describe PetsController do
describe "GET #index" do
it_behaves_like "an authenticated action", { get :index }
end
describe "PATCH #update" do
it_behaves_like "an authenticated action", { patch :update, { pet: { name: 'Moon Moon' }} }
end
describe "DELETE #destroy" do
it_behaves_like "an authenticated action", { delete :destroy, id: 1 }
end
endhttps://stackoverflow.com/questions/34188420
复制相似问题