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
4

1 回答 1

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]

希望这能让你开始。

于 2013-01-15T05:24:06.623 回答