Ruby   发布时间:2022-04-03  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了ruby-on-rails – 如何使用FactoryGirl发送参数(而不是手动将params作为哈希发送)?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我有以下rspec测试工作:
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

但是我使用Factory女孩,所以我不必手动创建api_keys.

如何复制上述功能,但使用工厂的女孩?

使用:

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值,当我到达我的控制器.

作为参,这里是我测试的测试的创建动作:

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

解决方法

尝试:
post :create,:api_key => FactoryGirl.attributes_for(:api_key)

大佬总结

以上是大佬教程为你收集整理的ruby-on-rails – 如何使用FactoryGirl发送参数(而不是手动将params作为哈希发送)?全部内容,希望文章能够帮你解决ruby-on-rails – 如何使用FactoryGirl发送参数(而不是手动将params作为哈希发送)?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。