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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T08:47:04+00:00 2026-05-12T08:47:04+00:00

I’ve pastied the specs I’ve written for the posts/show.html.erb view in an application I’m

  • 0

I’ve pastied the specs I’ve written for the posts/show.html.erb view in an application I’m writing as a means to learn RSpec. I am still learning about mocks and stubbing. This question is specific to the “should list all related comments” spec.

What I want is to test that the show view displays a post’s comments. But what I’m not sure about is how to setup this test and then have the test iterate through with should contain(‘xyz’) statements. Any hints? Other suggestions are also appreciated! Thanks.

—Edit

Some more information. I have a named_scope applied to comments in my view (I know, I did this a bit backwards in this case), so @post.comments.approved_is(true). The code pastied responds with the error “undefined method `approved_is’ for #”, which makes sense since I told it stub comments and return a comment. I’m still not sure, however, how to chain the stubs so that @post.comments.approved_is(true) will return an array of comments.

  • 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-05-12T08:47:05+00:00Added an answer on May 12, 2026 at 8:47 am

    Stubbing really is the way to go here.

    In my opinion, all objects in controller and view specs should be stubbed with mock objects. There is no real need to spend time redundantly testing logic that should already be thoroughly tested in your model specs.

    Here’s an example how I would set up the specs in your Pastie…

    describe "posts/show.html.erb" do
    
      before(:each) do
        assigns[:post] = mock_post
        assigns[:comment] = mock_comment
        mock_post.stub!(:comments).and_return([mock_comment])
      end
    
      it "should display the title of the requested post" do
        render "posts/show.html.erb"
        response.should contain("This is my title")
      end
    
      # ...
    
    protected
    
      def mock_post
        @mock_post ||= mock_model(Post, {
          :title => "This is my title",
          :body => "This is my body",
          :comments => [mock_comment]
          # etc...
        })
      end
    
      def mock_comment
        @mock_comment ||= mock_model(Comment)
      end
    
      def mock_new_comment
        @mock_new_comment ||= mock_model(Comment, :null_object => true).as_new_record
      end
    
    end
    
    • 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 have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
i got an object with contents of html markup in it, for example: string
I have an MVC Razor view @{ ViewBag.Title = Index; var c = (char)146;
I have thousands of HTML files to process using Groovy/Java and I need to
I am writing an app with both english and french support. The app requests
I'm working with an upstream system that sometimes sends me text destined for HTML/XML

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.