Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 107301
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T01:43:57+00:00 2026-05-11T01:43:57+00:00

I have the following controller action and test. I’m new to testing with Shoulda

  • 0

I have the following controller action and test. I’m new to testing with Shoulda and I know there are areas of my controller that I can test further. For example, my flash messages as well as verifying the renders.

So my question is, how would I properly test this controller action in Shoulda?

My controller action (names have been changed to protect the innocent):

def my_action   return redirect_to(root_url) if @site.nil?   @owner = current_site.owner   if request.post?     if params[:password].blank? || params[:email].blank?       flash[:error] = 'You must fill in both the e-mail and password fields'       render :action => 'my_action'     else       if @owner.authenticated?(params[:password])         @owner.login = params[:email]         @owner.save!         @owner.do_some_method         flash[:success] = 'Success.'         render :action => 'my_action'       else         flash[:error] = 'Incorrect password'         render :action => 'my_action'       end     end         end   end 

My test:

context 'on POST to :my_action' do   setup do     Owner.any_instance().expects(:do_some_method)     post :my_action, :email => 'foo@bar.com', :password => 'test'   end   should_assign_to :owner   should 'Change name and verify password and resend activation key' do     assert_equal true, assigns(:owner).authenticated?('test')     assert_equal 'foo@bar.com', assigns(:owner).login   end   should_respond_with :success end 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T01:43:57+00:00Added an answer on May 11, 2026 at 1:43 am

    Right now, it appears that you’re testing functionality specific to the model inside your controller, that should be in a unit test.

    I would advise re-factoring your controller to include the required logic for updating the Owner’s email inside the Owner model. By doing that, you should be able to simplify the controller down to a simple if update; else; end type statement and greatly simplify the controller test. Once you’ve moved the logic into the model, you can use built in Rails validations.

    A couple of other things to consider:

    • Redirecting after your POST action completes, prevents the user from double-posting by accident (most browsers will complain when the user attempts it).
    • Move the checking for @site and also the assignment to @owner to before_filters if this is done more than once inside the controller.
    • You can avoid having to check if request.post? with either verify or creating a route in `config/routes.rb’.

    References:

    • Skinny Controller, Fat Model
    • RailsConf Recap: Skinny Controllers
    • ActionController::Filters::ClassMethods
    • ActionController::Verification::ClassMethods
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Related Questions

Loading...

Sidebar

Ask A Question

Stats

  • Questions 54k
  • Answers 54k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer I'm using 2 for the scale since you said this… May 11, 2026 at 7:30 am
  • added an answer This might not be the answer you're looking for, but… May 11, 2026 at 7:30 am
  • added an answer You could create a Sql Server Agent Job that runs… May 11, 2026 at 7:30 am

Top Members

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.