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

  • Home
  • SEARCH
  • 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 9122987
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T06:13:55+00:00 2026-06-17T06:13:55+00:00

When using factory girl and RSpec to test my controller, I’m getting the error:

  • 0

When using factory girl and RSpec to test my controller, I’m getting the error:

Failures:

  1) ProductsController Handling POST for create should redirect to products after success
     Failure/Error: response.should redirect_to(products_path)
        Expected response to be a <:redirect>, but was <200>

Based on the log file, it looks like the object already exists in the DB (and doesn’t meet the redirect clause in the controller), which raises two questions:

  1. Why aren’t the calls getting mocked/stubbed?
  2. What would a workaround be if I wanted to hit the DB? Using @params = FactoryGirl.build(:product).attributes.except(:id, :created_at, :updated_at) doesn’t work either.

Any help would be awesome. Thank you.

test.log:

Processing by ProductsController#create as HTML
Parameters: {"product"=>{"name"=>"Fruit Mania", "description"=>"The mania that is taking over the planet!", "price"=>"9.99", "inventory"=>"1", "product_image"=>"#<File:0x00
User Load (0.3ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1
Product Exists (0.1ms)  SELECT 1 AS one FROM "products" WHERE LOWER("products"."name") = LOWER('Fruit Mania') LIMIT 1

products_controller_spec.rb:

describe ProductsController, "Handling POST for create" do
  def sign_in_user
    user = FactoryGirl.create(:user)
    sign_in user, @user
  end

  def do_post
    post :create, :product => @params
  end

  before do
    @product = mock_model(Product, :save=> true)
    Product.stub!(:create).and_return(@product)
    @params = FactoryGirl.attributes_for(:product)
  end

  it "should redirect to products after success" do 
     sign_in_user
     do_post
     pending "need to figure out why redirect fails"
     response.should redirect_to(products_path)
  end
end

Factory:

FactoryGirl.define do
  factory :product do
    name "Fruit Mania"
    description "The mania that is taking over the planet!"
    price "9.99"
    inventory 1
    product_image { File.open(File.join(Rails.root, 'spec', 'support', 'uploads', '1000x450.jpeg')) }
  end
end

Controller:

 def create
    @product = Product.new(params[:product])
    if !@product.valid?
      flash.now[:error] = ("The product was not added: <br><li>" + @product.errors.full_messages.join("<li>")).html_safe
      render "new"
    else
      if @product.save
        redirect_to products_path, :notice => "Product created successfully!"
      else
        flash.now[:error] = ("There were issues adding the product to the database. Please retry")
        render "new"
      end
    end
  end
  • 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-17T06:13:57+00:00Added an answer on June 17, 2026 at 6:13 am

    I can tell you why the database call is happening in the first place.

    You should replace this line:

    Product.stub!(:create).and_return(@product)
    

    with:

    Product.any_instance.stub!(:save).and_return(true)
    

    You are stubbing the class method Product::create, but you really need to stub the instance method Product#save.

    I suppose the following would also work:

    Product.stub!(:new).and_return(@product)
    @product.stub(:save).and_return(true)
    

    Because of the failure to save you go through the render path which gives an HTTP response of 200, not the 302 you were expecting.

    As to why it’s hitting the database, I’m afraid I can’t say why the model already exists from the given context. If you only run this single test, does it work? Try checking out this question for more ideas:

    Why isn't factory_girl operating transactionally for me? – rows remain in database after tests

    I hope that helps.

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

Sidebar

Related Questions

Using RSpec and Factory Girl, I create a record that when created, has associated
I'm using RSPEC and Capybara for testing and getting the following error. Failure/Error: click_button
I'm using RSpec + Shoulda to test my RESTful controller in Rails 3. I'm
i'm using Rspec and factory girl to set my tests. I have a user
Just wanted to post a quick thing I found about Factory Girl for others
I'm using rspec tests with shoulda matchers and autotest-rails. See gemfile: group :test do
I'm using minitest with factory girl and capybara for integration tests. Capybara works fine
I'm writing a basic application using RoR, and I'm testing with RSPec (and Factory
I am testing a controller in rails using rspec. The response body is just
I'm using Rspec, FactoryGirl and Capybara. When using ActiveSupport::TimeZone.us_zones. I run my test for

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.