我有以下有效的 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
但我使用工厂女孩,所以我不必手动创建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
当我到达我的控制器时,给我该值的空值。
作为参考,这是我的创建操作,该测试正在测试:
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