2013-02-26 13 views
9

Ben çalışır aşağıdaki RSpec test var:FactoryGirl ile params gönderme (paramleri karma olarak manüel olarak göndermenin tersine)?

it "redirects to the created api_key" do 
    post :create, :api_key => {:api_identifier => "asdfadsf", :verification_code => 
     "12345"} 
    response.should redirect_to(ApiKey.last) #(or any other test function) 
    end 

Ama elle api_key s oluşturmak zorunda kalmamak için ben Fabrika kızı kullanın.

Yukarıdaki işlevleri nasıl çoğaltabilirim, ancak fabrika kızını kullanabilir miyim?

kullanma:

it "redirects to the created api_key" do 
    test = FactoryGirl.build(:api_key) 
    post :create, :api_key => test 
    response.should redirect_to(ApiKey.last) #(or any other test function) 
    end 

ya: benim denetleyicisi vardığınızda

it "redirects to the created api_key" do 
    post :create, FactoryGirl.build(:api_key) 
    response.should redirect_to(ApiKey.last) #(or any other test function) 
    end 

bana :api_key değeri için boş değerler verir.

def create 
    @api_key = ApiKey.new(params[:api_key]) 
    @api_key.user = current_user 
    pp @api_key 

    respond_to do |format| 
    if @api_key.save 
     format.html { redirect_to @api_key, notice: 'Api key was successfully created.' } 
     format.json { render json: @api_key, status: :created, location: @api_key } 
    else 
     format.html { render action: "new" } 
     format.json { render json: @api_key.errors, status: :unprocessable_entity } 
    end 
    end 
end 

cevap

25

deneyin:

post :create, :api_key => FactoryGirl.attributes_for(:api_key) 
+0

Günümü siz yaptınız! Çok teşekkürler !! – Hito

1

aslında bir kaydı oluşturmaz build kullanma

Başvuru için, burada bu test test olduğunu benim yaratmak eylemdir. Sadece yaptığını iddia ediyor. attributes_for'u kullanmanız, bir nesnenin özelliklerini size verecektir. Bu çoğunlukla tanımladığınız bağlamda kullanılır. Bu da'un bir nesne oluşturmayacağını unutmayın.

Ne yapacağını yanıtı başarılı olması durumunda bu yönlendirme/geçerli:

response.should be_redirect 

Hatta daha da iyisi expect kullanın.

İlgili konular