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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T08:48:01+00:00 2026-06-09T08:48:01+00:00

I testing my app for create a new user car, later that the user

  • 0

I testing my app for create a new user car, later that the user creates the new car the app must redirect to the user_car_path (I post my routes):

user_cars GET    /users/:user_id/cars(.:format)                               cars#index
                      POST   /users/:user_id/cars(.:format)                               cars#create
         new_user_car GET    /users/:user_id/cars/new(.:format)                           cars#new
        edit_user_car GET    /users/:user_id/cars/:id/edit(.:format)                      cars#edit
             user_car GET    /users/:user_id/cars/:id(.:format)                           cars#show
                      PUT    /users/:user_id/cars/:id(.:format)                           cars#update
                      DELETE /users/:user_id/cars/:id(.:format)                           cars#destroy

so I’m testing my app with this rspec:

describe "POST 'create' car" do

describe "car created success" do
  before(:each) do
            @user = User.create!(:email => "foo@example.com", :password => "foobar", :password_confirmation => "foobar" )
            @car = Car.create!(:brand => "example", :color => "foobar", :model => "foobar", :year =>"2012")
  end

  it "should create a car" do
    lambda do
      post :create, :cars => @car, :user_id => @user.id
    end.should change(Car, :count).by(1)
  end

  it "should redirect to the user cars page" do
    post :create, :cars => @car, :user_id => @user.id
    response.should redirect_to user_car_path(@user, @car)
  end
end
end

but i got 2 errors

Failures:

1) CarsController POST 'create' car car created success should create a car
 Failure/Error: lambda do
   count should have been changed by 1, but was changed by 0
 # ./spec/controllers/car_controller_spec.rb:20

2) CarsController POST 'create' car car created success should redirect to the user cars page
 Failure/Error: response.should redirect_to user_car_path(@user, @car)
   Expected response to be a redirect to <http://test.host/users/115/cars/40> but was a redirect to <http://test.host/users/115/cars/new>.
 # ./spec/controllers/car_controller_spec.rb:27

but my app works normally; here is my CarController

class CarsController < ApplicationController
....

def create
  @user = User.find(params[:user_id])
  @car = @user.cars.build(params[:car])
  if @car.save
    redirect_to user_car_path(@user, @car), :flash => { :notice => "  car created!" }
  else
    redirect_to new_user_car_path ,:flash => { :notice => " sorry try again :(" }
  end
end
def show
  @user = User.find(params[:user_id])     
  @car = @user.cars.find(params[:id])   
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-09T08:48:03+00:00Added an answer on June 9, 2026 at 8:48 am

    You are creating the car in the database, rather than just creating a car object in the before(:each). Also you’re passing the param as :cars rather than :car. Finally, I’d also personally use let. Try this.

    describe "POST 'create' car" do
    
      let(:user) { User.create!(:email => "foo@example.com", :password => "foobar", :password_confirmation => "foobar" ) }
      let(:car)  { Car.new(:brand => "example", :color => "foobar", :model => "foobar", :year =>"2012")}
    
      it "should create a car" do
        lambda do
          post :create, :car => car, :user_id => user.id
        end.should change(Car, :count).by(1)
      end
    
      it "should redirect to the user cars page" do
        post :create, :cars => car, :user_id => user.id
        # note: car doesn't have an ID, you have to fetch it from the db to get an id
        response.should redirect_to user_car_path(user.id, user.cars.last.id)
      end
    end
    

    As a final note, you will want to look into Factory Girl

    Then you could do this instead:

    let(:user){ FactoryGirl.create(:user) }
    let(:car) { FactoryGirl.build(:car) } # note: BUILD not CREATE
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to build a small testing app with erlang+mnesia. I have a user
In testing an app, I keep getting errors that occur sporadically on only one
I'm testing an app that reads thousands of small objects and sends then back
I have to create an application for testing that is relatively simple. If a
I am attempting to create a simple app that can send a text message
Disclaimer: I'm very new to Django. I must say that so far I really
I have a test user account that is working just fine for testing in
I created an app and have been testing it against targetSdk 14. The spinner
I've created a Silverlight app and VS set up a web app for testing
I am testing my app in two different devices. My app is relies heavily

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.