我如何编写以下控制器的测试

时间:2013-01-10 19:44:04

标签: ruby rspec tdd bdd rspec2

我是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

1 个答案:

答案 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]

希望这能让你开始。