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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T12:38:47+00:00 2026-05-27T12:38:47+00:00

I have a test that fails and I just cannot figure out how to

  • 0

I have a test that fails and I just cannot figure out how to get it working.
It’s the it “should have the right title” test in the “POST Create” section.
It’s a Rails 3.1 app, heres my gem file for testing:

group :test do
  gem 'turn', :require => false
  gem 'minitest'
  gem 'spork', '> 0.9.0.rc'
  gem 'guard-spork'
  gem 'guard-rspec'
  gem 'webrat'
  gem 'factory_girl_rails', '> 1.0'
  gem 'mocha'
  gem 'rspec-rails'
end

My Posts Controller

class PostsController < ApplicationController

  def new
    @post = Post.new
    @title = "Create a new post"

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @post }
    end
  end

  def show
    @post = Post.find(params[:id])
    @title = @post.title

    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @post }
    end
  end

  def create
    @post = Post.new(params[:post])

    respond_to do |format|
      if @post.save
        format.html { redirect_to @post, notice: 'Post was successfully created.' }
        format.json { render json: @post, status: :created, location: @post }
      else
        format.html { render action: "new" }
        format.json { render json: @post.errors, status: :unprocessable_entity }
        @title = "Create a new post"
      end
    end
  end

end

Rspec tests – I have added render_views to file.

describe "POST 'create'" do

    describe "SUCCESS" do

      before(:each) do
        @attr = { :title => "Foo", :content => "Bar" }
      end

      it "should create a new post" do
        lambda do
          post :create, :post => @attr
          flash[:notice].should_not be_nil
        end.should change(Post, :count).by(1)
      end

      it "should redirect to the post show page and show success message" do
        post :create, :post => @attr
        flash[:notice].should =~ /post was successfully created/i
        response.should redirect_to post_path(assigns(:post))
      end

      it "should have the right title" do
        post :create, :post => @attr
        response.should have_selector("title", :content => assigns(:post).title)
      end
    end
end

Only the last test fails.
Error messages:

Finished in 6.49 seconds
11 examples, 1 failure

Failed examples:

rspec ./spec/controllers/posts_controller_spec.rb:62 # PostsController POST 'create' SUCCESS should have the right title
Running: spec/controllers/posts_controller_spec.rb
.......F...

Failures:

  1) PostsController POST 'create' SUCCESS should have the right title
     Failure/Error: response.should have_selector("title", :content => assigns(:post).title)
       expected following output to contain a <title>Foo</title> tag:
       <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
       <html><body>You are being <a href="http://test.host/posts/980190963">redirected</a>.</body></html>
     # ./spec/controllers/posts_controller_spec.rb:64:in `block (4 levels) in <top (required)>'

Any help as to why this fails would be greatly appreciated.

  • 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-27T12:38:48+00:00Added an answer on May 27, 2026 at 12:38 pm

    You’re willing something that simply won’t happen.

    Because you redirect, the response is:

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
    <html><body>You are being <a href="http://test.host/posts/980190963">redirected</a>.</body></html>
    

    It would be better to test the new object itself.

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

Sidebar

Related Questions

I have a JUnit test that fails because the milliseconds are different. In this
I have a test that is successful (among other things) if a certain callback
I have a JUnit test that tests adding Strings to a Dictionary custom type.
I have a unit test that works fine locally but when uploaded to TeamCity
I have a test file that contains tests taking quite a lot of time
I have a unit test that creates a mock calls my method to be
I have a test file that has many lines, each line looks something like:
I have a unit test that tests if an Exception is throw, but this
I have a test file that contains 1,2,3 2,3,4 5,6,7 I want to insert
I have this JUnit test that I need help developing a Interface and Class

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.