我有一個非常類似的設置,下面是我目前用來測試這個東西的代碼。在每個describe
S的我把:
it_should_behave_like "login-required object"
def attempt_access; do_post; end
如果你需要的是一個登錄,或
it_should_behave_like "ownership-required object"
def login_as_object_owner; login_as @product.user; end
def attempt_access; do_put; end
def successful_ownership_access
response.should redirect_to(product_url(@product))
end
如果你需要的所有權。很顯然,輔助方法在每一回閤中都會發生變化(很少),但是這對於你來說可以完成大部分工作。這是在我的spec_helper.rb
shared_examples_for "login-required object" do
it "should not be able to access this without logging in" do
attempt_access
response.should_not be_success
respond_to do |format|
format.html { redirect_to(login_url) }
format.xml { response.status_code.should == 401 }
end
end
end
shared_examples_for "ownership-required object" do
it_should_behave_like "login-required object"
it "should not be able to access this without owning it" do
attempt_access
response.should_not be_success
respond_to do |format|
format.html { response.should be_redirect }
format.xml { response.status_code.should == 401 }
end
end
it "should be able to access this if you own it" do
login_as_object_owner
attempt_access
if respond_to?(:successful_ownership_access)
successful_ownership_access
else
response.should be_success
end
end
end
最甜蜜的一個! – AnkitG 2013-10-10 15:16:05