2013-01-10 56 views
0

我是新來RSPEC,想知道我怎麼能寫功能測試用於以下控制器我如何我會寫測試以下控制器

class FeedbackFormsController < ApplicationController 

    before_filter :authenticate_user! 

def new 
    @feedback_form = FeedbackForm.new 
    session[:return_to] = request.referer 
end 

def create 
    feedback_form = FeedbackForm.new(params[:feedback_form]) 
    FeedbackMailer.new(feedback_form).deliver 
    flash[:notice] = "Your feedback was submitted successfully." 
    redirect_to session[:return_to] 
end 

end 
+0

恐怕你的問題非常模糊。我會創建至少5或6個rspec測試。請更具體一些。你讀過rspec文檔嗎?你有沒有嘗試過創建你的第一個rspec測試? – Geoff

+0

是的,我經常寫測試。但是,這一天,我陷入了無法避免測試代碼的最底層(我不知道它是對的,但是這使得我的測試非常僵硬)。所以問了問題,看看親紅寶石開發人員將如何編寫上述代碼的測試。 –

回答

2

兩個動作認爲它可能爲你做一個更好的學習體驗它是你自己,但我會讓你開始一些僞代碼。我會故意疏忽我的語法。

require spec-helper 

describe FeedbackFormsController 

    before each 
    controller should receive :authenticate_user! and return true 

    describe new 
    it should assign a new feedback form 
     get new 
     assigns feedback_form should be a new Feedbackform 

    it should call for the referer 
     request should receive referer 
     get new 

    it should set session value 
     request stub referer and return nonsense 
     expect 
     get new 
     to change session return_to to nonsense 

    describe create 
    it should create a new Feebackform 
     Feebackform should receive new with nonsense 
     post create nonsense 

    it should create a new Feebackmailer 
     mock = mock_model Feedbackform 
     Feedbackform stub new and return mock 
     Feedbackmailer should receive new with mock 
     post create nonsense 

    it should deliver a message 
     mock = mock_model FeedbackMailer 
     Feedbackform stub new 
     FeedbackMailer stub new and return mock 
     mock should receive deliver 
     post create nonsense 

    it should redirect 
     Feedbackform stub new 
     Feedbackmailer stub new 
     post create nonsense 
     response should redirect to session[return to] 

希望這應該讓你開始。

+0

謝謝!那裏有很多的學習。 –

相關問題