2014-03-13 66 views
0

目前,我有一些代碼如下所示:如何爲rspec測試創建模塊化結構?

it "Doesn't Access the Admin Panel" 

    it "Doesn't see New Project button" 

    it "Doesn't create new news" 

    it "Doesn't access New Project page" 

    it "Sees Projects" 

    it "Sees news" 

    it "Doesn't edit projects" 

    it "Doesn't edit news" 

正如你可以看到對幾乎所有測試的發生。是否有某種方式可以讓我進行這些測試,比如創建一個方法來接受一個參數,不管它是'新聞'還是'項目'?

這不是做事的方法嗎?我想遵循最佳實踐,而不是嘗試創建一些新的編碼風格。

回答

1

可以在鳥巢例子描述了使用反映上下文標題塊:

describe "When logged in as an admin" do 
    let(:user) { admin_user } 
    it "can view the project" do 
    # ... 
    end 
end 

describe "When logged in as a guest" do 
    let(:user) { guest_user } 
    it "cannot view the project" do 
    # ... 
    end 
end 

更多的例子見RSpec documentation