2013-04-17 39 views
0

這是我的rspec文件。我有PosRequest.authorize_card(@payment_detail)行3次。是否有DRYer方式來編寫此上下文?消除RSpec中的重複代碼上下文

context 'should get' do 
    it 'error message' do 
     PosRequest.authorize_card(@payment_detail) 
     @payment_detail.errorMsg.should_not eql(:nil) 
    end 
    it 'bank message' do 
     PosRequest.authorize_card(@payment_detail) 
     @payment_detail.cardMsg.should_not eql(:nil) 
    end 
    it 'claim message' do 
     PosRequest.authorize_card(@payment_detail) 
     @payment_detail.bankMsg.should_not eql(:nil) 
    end 
    end 

回答

0
context 'should get' do 
    before {PosRequest.authorize_card(@payment_detail)} 

    it 'sends error message' do 
    @payment_detail.errorMsg.should_not eql(:nil) 
    end 
    it 'sends bank message' do 
    @payment_detail.cardMsg.should_not eql(:nil) 
    end 
    it 'sends claim message' do 
    @payment_detail.bankMsg.should_not eql(:nil) 
    end 
end 

我編輯這讓你有什麼每個天賦是做一個清晰的概念加動詞。

1

裏面你context可以使用已經建議

before do 
    do_something 
end 

將只運行一次的所有測試這個上下文中,也可以使用

before :each do 
    do_something 
end 

,這將用於運行do_something一次每個測試都在這個背景下,你可以選擇更適合你的需求。