{:api_ide-6ren">
gpt4 book ai didi

ruby-on-rails - 如何使用 FactoryGirl 发送参数(而不是手动将参数作为散列发送)?

转载 作者:数据小太阳 更新时间:2023-10-29 06:47:46 25 4
gpt4 key购买 nike

我有以下有效的 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 girl,所以我不必手动创建 api_key

如何复制上述功能,但使用 factory girl?

使用:

  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

当我到达我的 Controller 时,给我 :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 发送参数(而不是手动将参数作为散列发送)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15093588/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com