1

新しいユーザーの車を作成するためにアプリをテストします。後でユーザーが新しい車を作成すると、アプリは user_car_path にリダイレクトする必要があります (ルートを投稿します)。

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

だから私はこの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

しかし、私は2つのエラーを得ました

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

しかし、私のアプリは正常に動作します。ここに私の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

....
4

3 に答える 3

4

で車のオブジェクトを作成するだけでなく、データベースで車を作成していますbefore(:each):carsまた、パラメータをではなくas として渡しています:car。最後に、私も個人的に使用しますlet。これを試して。

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

最後に、 Factory Girlについて調べてみましょう。

次に、代わりにこれを行うことができます:

let(:user){ FactoryGirl.create(:user) }
let(:car) { FactoryGirl.build(:car) } # note: BUILD not CREATE
于 2012-08-03T15:56:58.727 に答える
0

検証が失敗したためだと思います。コントローラーに保存する前に、失敗したかどうかを確認するには、次のようにします。

puts @car.valid?

false の場合、テストは失敗し、正常です。true に設定するには、次のようにします。

before(:each) do
  ...
  Car.any_instance.stub(:valid?).and_return(true)
end

スタブとモックを使用してUser.findとのインスタンスを返すこともできますCar.build。ドキュメントを参照してください: https://www.relishapp.com/rspec/rspec-mocks/v/2-6/docs/method-stubs

于 2012-08-03T15:34:19.607 に答える
0

私はあなたが次のことを望んでいると思います (:cars => :car で、コントローラーでの車の作成を有効にするパラメーターを使用します。これは車の作成のテストのように見えるため、仕様で車を作成する理由がわかりません)

  it "should create a car" do
    lambda do
      post :create, :car => {:brand => "example", :color => "foobar", :model => "foobar", :year =>"2012"}, :user_id => @user.id
    end.should change(Car, :count).by(1)
  end

  it "should redirect to the user cars page" do
    post :create, :car => {:brand => "example", :color => "foobar", :model => "foobar", :year =>"2012"}, :user_id => @user.id
    response.should redirect_to user_car_path(@user, @car)
  end
于 2012-08-03T15:31:22.383 に答える