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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T01:12:01+00:00 2026-06-01T01:12:01+00:00

I have following spec to test controller method: context #create do it should redirect

  • 0

I have following spec to test controller method:

context "#create" do
  it "should redirect when model is valid" do
    User.stub!(:valid?).and_return(true)
    post :create, :user => FactoryGirl.attributes_for(:user)
    response.should redirect_to("/")
  end
  it "should render new template when model is invalid" do
    User.stub!(:valid?).and_return(false)
    post :create, :user => FactoryGirl.attributes_for(:user)
    response.should render_template(:new)
  end
end

And controller itself:

def create
  @user = User.new(params[:user])
  if @user.save
    redirect_to "/", :notice => "User created"
  else
    render "new"
  end
end

Pretty much straightforward code, but somehow stub! method just doesn’t really stubs, so second spec fails with expecting <"new"> but rendering with <"">. It just redirects like if valid? returned true.

I’m quite new to Rails world. What am I missing? Thanks.

  • 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-01T01:12:03+00:00Added an answer on June 1, 2026 at 1:12 am

    When saving an object, Rails calls valid? on an instance of a class. But you’ve stubbed valid? on the class itself. That won’t work.

    What you want to do here is stub save on the instance of User that is being saved, e.g.

    User.stub(:new) { mock_model(User, :save => true) }

    @user = User.new # @user is now a mock object

    @user.save # mock object returns true

    For the other example:

    User.stub(:new) { mock_model(User, :save => false) }

    @user = User.new # again, @user is a mock

    @user.save # mock object returns false

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

Sidebar

Related Questions

I have a spec: it should redirect to the log_books_controller#new action when a user
In a branch spec, I have the following view: //depot/dev/t/a/g/... //depot/dev/t/r/g/... -//depot/dev/t/a/g/p/o*/... //depot/dev/t/r/g/p/... Perforce
I have following situation: I have loged user, standard authentication with DB table $authAdapter
I Have following code: Controller: public ActionResult Step1() { return View(); } [AcceptVerbs(HttpVerbs.Post)] public
I have the following XML example: <?xml version=1.0 encoding=UTF-8?> <OpenSearchDescription xmlns=http://a9.com/-/spec/opensearch/1.1/ xmlns:custom=http://example.com/opensearchextensions/1.0/> <ShortName>Test</ShortName> <Description>Testing....</Description>
I have the following spec (using Machine.Specifications or mSpec): public class when_a_user_logs_in_successfully { static
I have the following code(simplified). public class OrderProcessor { public virtual string PlaceOrder(string test)
I have an Rspec test for a helper method which requires access to my
I am trying to test my controller's create action. I am using Devise for
My rspec title test is failing with: 1) HomeController Get 'index should have the

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.