2011-02-07 27 views
1

此問題是關於如何最好地命名RSpec示例組和英語示例。用於webapps的RSpec示例組命名

我明白,在RSpec中,describecontext(這是functionally equivalent)和it應該給你完整的句子。因此,例如,

describe "Log-in controller" do 
    context "with logged-in user" do 
    it "redirects to root" do 
     ... 
    end 
    end 
end 

讀數爲Log-in controller with logged-in user redirects to root。真棒。

但在我的應用程序,在那裏我需要測試的ajaxy網頁上的所有組件(使用水豚),我傾向於有例如組這樣的:

describe "blog post" do 
    describe "content" do 
    it "is displayed" 
    end 
    describe "comment" do 
    it "is displayed" 
    describe "editing" do 
     it "works" # successful comment editing 
     it "requires logged-in user" # error case 1 
     it "requires non-empty body" # error case 2 
    end 
    end 
    describe "comment form" do 
    it "works" # successful comment submission 
    it "requires valid email address" # error case 1 
    it "requires non-empty body" # error case 2 
    end 
end 

我看到兩個在這裏反模式:

  1. 嵌套描述不讀爲句子。當然,人們可以把一個「s:

    describe "blog post" do 
        describe "'s content" do 
        it "is displayed" 
        end 
    end 
    

    或之後,一個可以把冒號‘的博客文章:’。或者理想,我會寫

    describe "blog post" do 
        its "content" do 
        it "is displayed" 
        end 
    end 
    

    但那是不可能的,因爲its約爲屬性訪問,我只是有一個字符串在這裏。

    有沒有更好的方法來處理「頁面組件」問題?

  2. 對於功能,成功案例(用於評論提交等功能)僅標記爲it "works"。至少這是簡潔而簡單的 - 我發現它略好於it "can be submitted and causes a comment to be added",因爲這只是迫使我爲明顯的東西組成了一個詞組。但是有沒有更好的,更「自然」的方式來做到這一點?

有關如何重構示例示例組的建議;)以上將不勝感激!

回答

1

你不應該真的考慮讓例子在語法上是正確的。如果您的測試顯示'博客文章內容顯示'沒有'',那很好。測試是可讀和簡單的。你真正想要的是能夠理解什麼是測試失敗時失敗。

關於你的第二點,'它的作品'通常不夠描述。它不會讓別人知道你的意思是'作品'。如果你真的在測試很多東西,最好把你的例子分開,例如:

 
describe 'blog post' do 
    context 'creating a comment' do 
    it 'should require a logged-in user' 
    it 'should require a non-empty body' 
    it 'should require a valid email address' 
    it 'should create a new comment' 
    it 'should be submittable' 
    end 
end 
+0

有趣!我不確定我是否確信你的示例將它分解爲「它」起作用「':」應該提交「實際上不是一個有趣的測試(它只需要一個按鈕),諸如」創建新評論「,」新評論有正確的作者「等等通常只能通過複製代碼和失去性能來分裂(這對Selenium來說真的很痛苦)。因此,對於成功的案例,我通常會應用KISS,並且只是對照我所關心的事物行使功能和一些期望。我真的沒有看到我的規格的可維護性通過分解這些來提高。 –

+0

我的僞代碼僅作爲指導。 '應該提交'僅僅是你應該如何分割'可以提交併且導致評論被創建'的一個例子。我的觀點是,你想爲每個測試提供不同的測試條件。一旦你開始爲你的描述添加'和',他們需要分裂。如果您使用Silenium進行測試,我不建議進行100%覆蓋。您可以首先使用模型和控制器測試完成同等水平的覆蓋率。如果您擔心性能和重複,請創建背景條件以避免這些問題。 –

+0

我並不確信分裂我的測試會使它們更易於維護。當你有類似「提交評論,檢查它通過JS動態添加,檢查它是否有正確的作者,檢查它有一個日期,檢查它是可編輯的,檢查它仍然是在頁面重新加載後在頁面上」,是你真的主張把它分成幾個測試(而不是簡單的期望)?順便說一下,「背景條件」是什麼意思? ':speed =>:slow'種類的元數據? –