Reputation: 6862
I am new to rspec and was wondering how could I write functional test for following two action of controller
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
Upvotes: 0
Views: 90
Reputation: 2228
I think it's probably a better learning experience for you to do it yourself, but I'll get you started with some pseudo-ish code. I will be purposely lax with my syntax.
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]
Hopefully that should get you started.
Upvotes: 2