2011-05-12 3 views
3

CanCan과 Devise가 잘 작동 했으므로 이제 테스트에 추가해야합니다.컨트롤러 사양의 장치 및 캔 캔

테스트 횟수를 두 배로 늘리겠습니까? 모든 것을 "게스트"사용자로 테스트 한 다음 사용자 및 관리자로 테스트해야합니다.

rspec을 사용하면 어떻게 배치할까요?

describe "GET edit" do 
    login_admin 
    it "assigns the requested forum_topic as @forum_topic" do 
     ForumTopic.stub(:find).with("37") { mock_forum_topic } 
     get :edit, :id => "37" 
     response.should redirect_to(new_user_session_path) 
    end 

    it "assigns the requested forum_topic as @forum_topic" do 
     ForumTopic.stub(:find).with("37") { mock_forum_topic } 
     get :edit, :id => "37" 
     assigns(:forum_topic).should be(mock_forum_topic) 
    end 
end 

도우미 모듈

능력 파일 자체를 테스트하고, 캉캉을 테스트 할 때 일반적으로 이루어집니다 무엇
def login_admin 
    before(:each) do 
     @request.env["devise.mapping"] = Devise.mappings[:admin] 
     sign_in Factory.create(:admin) 
    end 
    end 

    def login_user 
    before(:each) do 
     @request.env["devise.mapping"] = Devise.mappings[:user] 
     @user = Factory.create(:user) 
     sign_in @user 
    end 
    end 

답변

2

. 예를 들어

당신은 당신과 같이 그것을 테스트 할 수 서명하지 않는 한이 앱에 당신이 특정 포럼을 볼 수 없습니다해야 테스트하는 경우 :

@user = Factory.create(:user) 
@forum = Factory.create(:forum) 

describe "User ability" do 
    it "Should be able to view forums" do 
    @ability = Ability.new(@user) 
    @ability.should be_able_to(:show, @forum) 
    end 
end 

describe "nil ability" do 
    it "Should be not be able to view forums if not signed in" do 
    @ability = Ability.new(nil) 
    @ability.should_not be_able_to(:show, @forum) 
    end 
end 

이 그냥 예입니다.

당신이 그것에 대해 더 읽을 수 있습니다 : https://github.com/ryanb/cancan/wiki/Testing-Abilities

을 그리고 마지막으로 그냥 카피 바라와 통합 테스트를 고안 테스트하고 관리자 및 사용자로 로그인 할 수 있습니다.