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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T21:31:05+00:00 2026-06-04T21:31:05+00:00

I need some help guys, trying to make this test to pass but with

  • 0

I need some help guys, trying to make this test to pass but with no luck.

describe 'PUT posts/:id' do 

  describe 'with valid attributes' do

    let(:mock_post) { mock_model('Post', title: 'hey! iam a mock!', description: 'a sexy model', location: 'everywhere') }

    login_user

    it 'should update the object and redirect to the post' do 
      Post.stub!(:find).with(mock_post.id).and_return(mock_post)

      Post.any_instance.should_receive(:update_attributes).with({"these" => "params"}).and_return(true)

      response.should redirect_to post_path(mock_post)

      put :update, id: mock_post.id, post: { these: 'params' }
    end

    it 'should have a current_user' do 
      subject.current_user.should_not be_nil
    end

  end

For now, I have something like the above test and getting the following error:

1) PostsController PUT posts/:id with valid attributes should update the object and redirect to the post
     Failure/Error: response.should redirect_to post_path(mock_post)
       Expected response to be a <:redirect>, but was <200>
     # ./spec/controllers/posts_controller_spec.rb:200:in `block (4 levels) in <top (required)>'

PostsController:

class PostsController < ApplicationController
  load_and_authorize_resource except: [:index, :show]
  before_filter :authenticate_user!, except: [:index, :show, :tags]
  before_filter :find_post, only: [:show, :edit, :update, :suspend, :suspend_alert]

def update
  if @post.update_attributes(params[:post])
    flash[:success] = 'Cool.'
    redirect_to post_path(@post)
  else
    render :edit
  end
end

protected
  def find_post
    @post = Post.find(params[:id])
  end
end

Also, how should I write the test for the render :edit part?

  • 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-04T21:31:06+00:00Added an answer on June 4, 2026 at 9:31 pm

    Your spec never calls the controller action. Try adding:

    Post.any_instance.
      should_receive(:update_attributes).
      with({"these" => "params"})
    put :update, :id => "1", :post => {"these" => "params"}
    

    To test the two paths that result from the call to update_attributes, substitute the value in the expectation:

    it "should redirect when successful" do
      Post.any_instance.
        should_receive(:update_attributes).
        with({"these" => "params"}).
        and_return(true)`
      response.should_redirect_to(post_path(@mock_post))
      put :update, :id => "1", :post => {"these" => "params"}
    end
    
    it "should render the edit page when unsuccessful" do
      Post.any_instance.
        should_receive(:update_attributes).
        with({"these" => "params"}).
        and_return(false)`
      response.should render_template("edit")
      put :update, :id => "1", :post => {"these" => "params"}
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hello everyone i need some help. I'm trying to make an application for mac
Hey guys I need some help with this: I have two view controllers, let's
Need some help to solve this. I have a gridview and inside the gridview
Need some help with this problem in implementing with XSLT, I had already implemented
Need some help gathering thoughts on this issue. Our team is moving ahead with
hi all i need some help from you guys.. well here's the description Guys
I am working on an iPad application and need some help from you guys.
Hi guys I need some help with Apple App Store. I've an application that
I need some help about mod_rewrite, this is my wordpress address: http://localhost/my_v2010/restaurants?m=display_Add base_url: http://localhost/my_v2010/
I need some help from you guys. I have a string name = john

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.