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 8108481
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T01:07:20+00:00 2026-06-06T01:07:20+00:00

I’m currently struggling a bit trying to keep my controller specs DRY and succinct

  • 0

I’m currently struggling a bit trying to keep my controller specs DRY and succinct and down to one assertion per example. I’m running into some difficulties particularly with where to place the actual controller request call within a structure nested to match the various edge cases.

Here’s an example, simplified to demonstrate the problem:

describe MyController do
  let(:item) { Factory(:item) }
  subject { response }

  describe "GET #show" do
    before(:each) do
      get :show
    end

    context "published item" do
      it { should redirect_to(success_url) }
    end

    context "unpublished item" do
      before(:each) do
        item.update_attribute(published: false)
      end

      it { should redirect_to(error_url) }
    end
  end
end

Clearly this is a contrived example, but it illustrates what I’d like to do and what’s not working. Mainly, the before block under the “unpublished” context is the problem. What happens is the change I made to the setup data actually happens after the get call due to the way the contexts are nested, so the example in that context is actually working with the initial scenario rather than the one I intend.

I understand why this happens and how contexts nest. I guess what I’d like to have is some way to tell RSpec what I’d like it to run right after any before hooks yet right before any assertions within a given context. This would be perfect for controller specs. I’d like to take advantage of nesting in my controller specs to gradually build up variations of edge cases without having to scatter the get call or even a call to a do_get helper into each of my it assertions. This would especially get annoying to keep in sync with any custom it_should macros I’m using.

Is there anything in RSpec currently to accomplish this? Are there any tricks I can use to get close? It would seem perfectly suited to the way I’ve seen a lot of people writing their controller specs; from what I’ve found, people have basically settled for having do_get helpers called before every assertion. Is there a better way?

  • 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. Editorial Team
    Editorial Team
    2026-06-06T01:07:23+00:00Added an answer on June 6, 2026 at 1:07 am

    The DRY principle states that “Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.” What you’re doing is much more about saving a few characters here and there than keeping things DRY, and the result is a tangled web of dependencies up and down a hierarchy that, as you can see, is a bitch to get to do what you want it to and consequently fragile and brittle.

    Let’s start with what you’ve got written out in a way that’s verbose and works:

    describe MyController do
      describe "GET #show" do
        context "published item" do
          it "redirects to the success url" do
            item = Factory(:item, published: true)
            get :show, :id => item.id
            response.should redirect_to success_url
          end
        end
    
        context "unpublished item" do
          it "redirects to the error url" do
            item = Factory(:item, published: false)
            get :show, :id => item.id
            response.should redirect_to error_url
          end
        end
      end
    end
    

    Now the only “pieces of knowledge” that are being duplicated are the names of the examples, which could be generated by the matchers at the end of each example. This can be resolved in a readable way by using the example method, which is an alias of it:

    describe MyController do
      describe "GET #show" do
        context "published item" do
          example do
            item = Factory(:item, published: true)
            get :show, :id => item.id
            response.should redirect_to success_url
          end
        end
    
        context "unpublished item" do
          example do
            item = Factory(:item, published: false)
            get :show, :id => item.id
            response.should redirect_to error_url
          end
        end
      end
    end
    

    There. DRY. And quite readable and easy to change. Now, when you happen to add more examples for either of the contexts, you can add a let:

    describe MyController do
      describe "GET #show" do
        context "published item" do
          let(:item) { Factory(:item, published: true) }
          example do
            get :show, :id => item.id
            response.should redirect_to success_url
          end
    
          example do
            # other example
          end
        end
        # ...
      end
    end
    

    Now the only duplicated code (not the same as the DRY principle) is the get. If you really feel strongly about it, you can delegate those calls out to a method like get_show(id) or some such, but it’s not really buying much at that point. It’s not like the API for get is going to change from under you, and the only argument to get is the item‘s id, which you actually care about in the example (so there’s no unnecessary information).

    As for using subject to capture the response and get one-liners out of the deal, that just makes things really difficult to read and doesn’t save you much. In fact, I’ve come to consider using subject in this way to be a smell.

    Hope this all helps.

    Cheers,
    David

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I want use html5's new tag to play a wav file (currently only supported
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I am currently running into a problem where an element is coming back from
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out

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.