2013-02-26 23 views
9

は私が働き、以下のRSpecのテストがあります。FactoryGirlでパラメータを送信するには(手動でパラメータをハッシュとして送信するのではなく)

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 

をしかし、私は手動でapi_key Sを作成する必要はありませんので、私は工場の女の子を使用しています。

私は上記の機能を複製できますが、工場の女の子は使用できますか?

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 

か:使用

私は私のコントローラに到着したとき

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 

は私に:api_key値にnull値を与えます。

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 

答えて

25

試し:

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

あなたは私の一日を作った!どうもありがとう !! – Hito

1

が実際にレコードを作成しませんbuildを使用して

は参考のため、ここでは、このテストはテストであることを私の作成アクションです。それはちょうどそれがしたふりをする。 attributes_forを使用すると、オブジェクトの属性が得られます。これは主にあなたが記述する文脈で使用されます。 this tooもオブジェクトを作成しません。

response.should be_redirect 

あるいはさらに良いexpectを使用します。私はどうなるのか

は応答が成功した場合/リダイレクトこのです。

関連する問題