2014-10-18 105 views
0

後,我有這樣一個規範:Rspec的:測試塊

context 'index' do 
    let!(:article) { create :article } 
    subject { visit articles_path } 

    specify do 
    subject 
    expect(page).to have_content(article.title) 
    end 
end 

然而,當我試圖重構它這樣,它說我有0例子:

context 'index' do 
    let!(:article) { create :article } 
    subject { visit articles_path } 

    context do 
    after { expect(page).to have_content(article.title) } 
    end 

    context do 
    before { login_as :user } 
    after { expect(page).to have_content(article.comment) } 
    end 

    context do 
    after {} 
    end 

end 

不應該它運行subject,然後after掛鉤?我很確定我以前使用過這個設置。

請告訴我如何正確重構這個。謝謝。

更新 能做到這樣,但我真的不喜歡它:

subject do 
    visit articles_path 
    page 
end 

specify do 
    expect(subject).to have_content(article.title) 
end 

回答

0

「後」塊通常使用的測試例執行後做一些事情。在第二個例子中,你有沒有測試之前的塊「後」

我會重構這樣

context 'index' do 
    let!(:article) { create :article } 
    visit articles_path 
    expect(page).to_not have_content(category.title) 
end 

編輯你的代碼執行

context 'index' do 
    let!(:article) { create :article } 

    before do 
    visit articles_path 
    end 

    context do 
    it "displays article's title" do 
     expect(page).to have_content(article.title) 
    end 
    end 

    context do 
    before { login_as :user } 
    it "displays article's comments" do 
     expect(page).to have_content(article.comment) 
    end 
    end 

    context do 
    #another test 
    end 
end 
+0

事情是我有更多的上下文共享相同的'let!/ visit'共享上下文。我認爲我可以在'context'中做'實際測試','expect after {expect}';結束' – firedev 2014-10-18 11:56:14

+0

請參閱我編輯的解決方案,我不知道你爲什麼要將驗證碼放在after塊之後。 – Alireza 2014-10-18 12:48:32

+0

在這種情況下'{login_as:user}'之前的'將在{before {visit articles_path}之前執行,並且爲了使您的示例工作,我必須在{login_as:user;訪問'context'塊中的articles_path}。 – firedev 2014-10-18 13:47:59

0

在我看來,從你重構嘗試有點混亂。

首先,您看到0 examples, 0 failures的原因是因爲測試中沒有主題調用。想想是這樣的:

subject { visit articles_path } 

it 'has a nice title' do 
    subject 
    expect(page).to have_content(article.title) 
end 

it 'has a nice comment' do 
    subject 
    expect(page).to have_content(article.comment) 
end 

爲了您的期望工作,您需要調用該主題。事實上,你可以通過在你的it/specify塊寫明確visit articles_path

it 'has a nice title' do 
    visit articles_path 
    expect(page).to have_content(article.title) 
end 

it 'has a nice comment' do 
    visit articles_path 
    expect(page).to have_content(article.comment) 
end 

測試共享相同的主題可以用subject { ... }竭使用主題甚至避免。

其次,不要混淆context塊和specify/it塊(記住它們是別名)。 A context是一種通過爲測試分離不同結果來讓測試更易於理解的方法。

subject { visit articles_path } 

context 'user is logged in' do 
    it 'displays article's title' do 
    login_as :user 
    subject 

    expect(page).to have_content(article.title) 
    end 

    it 'displays article's title' do 
    login_as :user 
    subject 

    expect(page).to have_content(article.comment) 
    end 
end 

context 'user not logged in' do 
    it 'displays article's comments' do 
    subject 

    expect(page).to have_content('Log in') 
    end 
end 

你可以有不同的期望,它/在相同的上下文中指定塊。 使用不同的上下文來指定相同功能的不同行爲。

最後最後一步。在before塊中分組共享功能。在我們的例子:

subject { visit articles_path } 

before do 
    subject 
end 

context 'user is logged in' do 

    before do 
    login_as :user 
    end 

    it 'displays article's title' do 
    expect(page).to have_content(article.title) 
    end 

    it 'displays article's title' do 
    expect(page).to have_content(article.comment) 
    end 
end 

context 'user not logged in' do 
    it 'displays article's comments' do 
    expect(page).to have_content('Log in') 
    end 
end 

正如你可以看到,這兩個上下文中運行的問題,但只有第一個內容登錄的用戶,而第二上下文不測試文章頁面。 我希望這是有用的。

繼續測試,很快就會成爲習慣問題,您將很容易編寫測試。