2011-08-28 18 views
0

我目前使用cancan的rspec。我意識到,亂拋垃圾的權限控制測試用例遍佈我的控制器規格文件是非常混亂。基本上,我的幾乎每一個控制器規格文件沿着線的東西:Rspec組織

describe "failure" do 
    it { get :new } 
    it { get :edit, :id => @deal } 
    it { get :update, :id => @deal } 
    it { get :destroy, :id => @deal } 

    after(:each) do 
     response.should_not be_success 
     response.should redirect_to(root_path) 
     flash[:error].should == "Permission denied." 
    end 
    end 
end 

我在我的系統4個角色,這絕對讓組織更加艱鉅的任務。

由於所有這些測試都與權限控制/ ACL,我試圖把他們都在一個文件中的RSpec /模型/ ability_spec.rb

現在,我ability_spec看起來是這樣的:

describe "cancan" do 
    it "failure" do 
    @ability.should_not be_able_to(:all, Factory(:purchase)) 
    @ability.should_not be_able_to(:all, Factory(:user)) 
    @ability.should_not be_able_to(:all, Visit) 
    end 
end 

我收到以下錯誤:

6) Ability consumers deals failure 
    Failure/Error: it { get :destroy, :id => @deal } 
    NoMethodError: 
     undefined method `get' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_1::Nested_2::Nested_2:0x007fd73209a270> 
    # ./spec/models/ability_spec.rb:46:in `block (5 levels) in <top (required)>' 

我知道我不應該把控制器GET/POST在這個文件中。有沒有辦法做到這一點,以簡化測試我的權限相關的測試?

+0

無關你的問題,但你的規格是不是非常具有可讀性,他們不告訴我(作爲一個人)你所描述的。 '它{get:some_action}'什麼也沒描述。你應該使用自然語言表達來描述你的代碼。 – d11wtq

回答